sails-postgresql "poolSize" too high error when connecting to database running in Docker container

1k views Asked by At

I'm building a new Sails.js application that connects to a PostgreSQL database. The application used to run just fine until I created a new model using sails generate model products. I now run into the following error when the application tries to start:

server_1      | [nodemon] starting `node app.js`
server_1      |  info: ·• Auto-migrating...  (alter)
server_1      |  info:    Hold tight, this could take a moment.
server_1      | Troubleshooting tips:
server_1      |
server_1      |  -> Is your Postgresql configuration correct?  Maybe your `poolSize` configuration is set too high? e.g. If your Postgresql database only supports 20 concurrent connections, you should make sure you have your `poolSize` set as something < 20 (see http://stackoverflow.com/a/27387928/486547). The default `poolSize` is 10. To override default settings, specify the desired properties on the relevant Postgresql "connection" config object where the host/port/database/etc. are configured. If you're using Sails, this is generally located in `config/datastores.js`, or wherever your environment-specific database configuration is set.
server_1      |
server_1      |  -> Maybe your `poolSize` configuration is set too high? e.g. If your Postgresql database only supports 20 concurrent connections, you should make sure you have your `poolSize` set as something < 20 (see http://stackoverflow.com/a/27387928/486547). The default `poolSize` is 10.
server_1      |
server_1      |  -> Do you have multiple Sails instances sharing the same Postgresql database? Each Sails instance may use up to the configured `poolSize` # of connections. Assuming all of the Sails instances are just copies of one another (a reasonable best practice) we can calculate the actual # of Postgresql connections used (C) by multiplying the configured `poolSize` (P) by the number of Sails instances (N). If the actual number of connections (C) exceeds the total # of **AVAILABLE** connections to your Postgresql database (V), then you have problems.  If this applies to you, try reducing your `poolSize` configuration. A reasonable `poolSize` setting would be V/N.

In response to this error, I added a poolSize key to my datastores.js file like this:

module.exports.datastores = {

  default: {
    adapter: 'sails-postgresql',
    url: 'postgresql://postgres@postgresql:5432/web-service',
    poolSize: 1
  },
};

It still throws the same error. Both the application and database are running in a Docker environment. This is what my docker-compose.yml looks like:

version: '3.4'

services:
  server:
    image: web-service
    build: .
    environment:
      NODE_ENV: development
      IS_DEV_MACHINE: "yes"
    ports:
      - 1338:1337 # HOST_PORT is 1338 to avoid conflicts with other Sails.js apps running on host
    volumes:
      - ../:/usr/src/app
    entrypoint: nodemon

  postgresql:
    image: bitnami/postgresql:9.6
    ports:
      - 5433:5432 # HOST_PORT is 5433 to avoid conflicts with other PostgreSQL instances running on host
    volumes:
      - ../database:/bitnami

I can't seem to find a solution to this anywhere. The other StackOverflow threads about this subject haven't been answered in more than a year. Any help appreciated.

1

There are 1 answers

0
JackH On

I managed to resolve the error by switching from the bitnami:postgresql:9.6 Docker image to postgres:9.6. Apparently postgres:9.6 accepts 100 connections (which appears to be more than what the Bitnami image accepts). Sails no longer throws the poolSize error even if I remove poolSize: 1 from datastores.js. This is what my docker-compose.yml file looks like now

version: '3.4'

services:
  server:
    image: web-service
    build: .
    environment:
      NODE_ENV: development
      IS_DEV_MACHINE: "yes"
    ports:
      - 1338:1337 # HOST_PORT is 1338 to avoid conflicts with other Sails.js apps running on host
    volumes:
      - ../:/usr/src/app
    entrypoint: nodemon

  postgresql:
    image: postgres:9.6
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
    ports:
      - 5433:5432 # HOST_PORT is 5433 to avoid conflicts with other PostgreSQL instances running on host
    volumes:
      - ../database:/var/lib/postgresql/data