Error running strapi with postgres in production
Describe the bug Strapi is unable to start correctly with PM2 and postgres configuration.
Steps to reproduce the behavior
1 - Follow the deployment guide for aws, and configure ...production/database.json like in the guide: https://strapi.io/documentation/3.0.0-beta.x/deployment/amazon-aws.html#_2-edit-the-database-json-file
2 - Create the PM2 ecosystem-config.js like in the guide, exporting the env variables to connect to a postgres RDS instance.
3 - Run the app and the following pm2 logs shows:
0|strapi | [2020-05-24T19:02:06.951Z] debug ⛔️ Server wasn't able to start properly.
0|strapi | [2020-05-24T19:02:06.953Z] error TimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
0|strapi | at /home/ubuntu/Applications/strapi-demo/node_modules/knex/lib/client.js:318:17
4 - The problem seems to arise from the database production configuration:
...
"connector": "bookshelf",
"settings": {
"client": "postgres",
...
},
...
If client is changed to sqlite, everything goes ok, and the database used is indeed a Postgres instance.
Expected behavior
We should have the client configured as postgres.
System
- Node.js version: v14.1.0
- NPM version: 6.14.4
- Strapi version: 3.0.0-beta.20.3
- Database: Postgres 10.6
- Operating system: Ubuntu
I'm having the same issue.
Add option like :
....
"options": {
"pool": {
"min": 0,
"max": 15,
"idleTimeoutMillis": 30000,
"createTimeoutMillis": 30000,
"acquireTimeoutMillis": 30000
}
}
@bellib, if these options solve the problem, then maybe these should be the default? WDYT?
@alexandrebodin the amount of times I have seen this issue come in the past week is growing quite a bit. We should entertain digging into this more (else we should maybe pass along some of the tarn stuff by default because it's apparent that most users are not using local databases, and thus their latency is too high).
Alternatively there is some massive unoptimization being introduced.
@alexandrebodin @derrickmehaffy, i'm sure there's a rational behind this, but shouldn't strapi stick with the knex defaults of 60000ms? (Although tarn has more permissive defaults, but strapi uses knex)
I'm connecting to an RDS instance, inside an aws VPC, which is not an edge case.
tbh I'm honestly not sure why in the world it would be set so low. (as a side point the pool settings are passed from knex to https://github.com/vincit/tarn.js/ as knex doesn't directly handle the pools)
And the min connection pool set to 0 may also be changed. Anyway, it's a very sensitive area, but knex defaults sound kind of save. @derrickmehaffy, I do understand knex uses tarn under the wood, but IMO, since strapi uses the knex abstraction, it would be much safer to pass the knex defaults and not the tarn defaults. Imagine knex has some issue and changes a pool configuration default, strapi will handle that gracefully.
And the min connection pool set to
0may also be changed. Anyway, it's a very sensitive area, but knex defaults sound kind of save. @derrickmehaffy, I do understandknexusestarnunder the wood, but IMO, since strapi uses theknexabstraction, it would be much safer to pass theknexdefaults and not thetarndefaults. Imagineknexhas some issue and changes a pool configuration default, strapi will handle that gracefully.
That sounds like a good idea, if you are up for it do you want to create a PR to change the defaults (at the same time too, it might be good to review the depends and update to the newer version of knex if we can. Not too sure about any breaking changes, but I do remember reading the knex changelog including some transaction fixes I believe :thinking: )
The PR would be basically to remove this code: https://github.com/strapi/strapi/blob/master/packages/strapi-connector-bookshelf/lib/knex.js#L111, but I would like to have more insights on why connections other than sqlite gets the config so ripped off. Maybe @alexandrebodin can give some insights, the commit history where this code is introduced it's not a good source, seams like copy/rewrite from other places: https://github.com/strapi/strapi/commit/66b2d4f5c3b4aabfed4eeaff10d0122f9b720430
I also followed the instructions in the AWS postgres deployment guide, and I'm seeing this same issue. I tried modifying options as shown above, but it doesn't seem to help (other than making the ultimate Server wasn't able to start properly. message take longer to appear).
@probityrules, i think that, if it's a connection timeout issue, you can handle it by tweaking the pool configuration. Just check this code: https://github.com/strapi/strapi/blob/master/packages/strapi-connector-bookshelf/lib/knex.js#L111, connections other than sqlite, will get these defaults.
Do this, pass the client option as sqlite, even if postgres. Them, if ok, update with postgres, but set the poll options like the knex and tarn libs. Check github repos.
@abelsoares tried change the client to sqlite Strapi run but never connect to my PG RDS still use sqlite local db, its very frustrating, I spent a lot of hours trying to deploy instead of develop my app :(
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'postgres',
host: env('DATABASE_HOST', 'XXXXXrds.amazonaws.com'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'XXXX'),
username: env('DATABASE_USERNAME', 'XXXX'),
password: env('DATABASE_PASSWORD', 'XXXXX'),
},nano
options: {
ssl: false,
pool:{
min: 0,
max: 15,
acquireTimeoutMillis: 2000,
idleTimeoutMillis: 2000,
createTimeoutMillis: 30000,
acquireTimeoutMillis: 1000
}
},
},
},
});
@dangercris If you've read all the comments in this issue, you'll get that you need to tune the connection pool settings. Try a higher acquireTimeoutMillis.
@dangercris If you've read all the comments in this issue, you'll get that you need to tune the connection pool settings. Try a higher
acquireTimeoutMillis.
Thanks for the response, what will be a great number for this, of what is the calculation methood?
@dangercris try the knex defaults:
... acquireConnectionTimeout defaults to 60000ms and is used to determine how long knex should wait before throwing a timeout error...
And you should be fine.
Okay, my solution was different although it exhibits this same error: I seemed to have applied a security group to the postgres db that blocked the 5432 port. I guess I made a misstep from the guide, but opening that port fixed the issue for me. (Thanks to this comment for the solution.)
Honestly Strapi team really need to sort out these stupid deployment issues or else people are going to find another CMS to use. This will be the last time I use it.