Launching multiple instances of PostGraphile with PM2

PM2 allows you to restart your application instances if they stop for any internal reason. A typical way to launch the PostGraphile with PM2 is as below:

pm2 start --name "postgraphile" postgraphile -- --append-plugins postgraphile-plugin-connection-filter -c postgres://user:password@localhost:15432/db -s public -a -j -n 0.0.0.0 -p 15433 --enhance-graphiql --disable-query-log --no-ignore-indexes --simple-collections both

The postgraphile-plugin-connection-filter plugin can be installed with command as below:

npm install -g tslib postgraphile-plugin-connection-filter postgraphile
Launching multiple instances of PostGraphile with PM2

However, if you have multiple databases that need PostGraphile, then you would have to start multiple instances of it and the way to achieve with PM2 is to have a configuration file as below:

// content of pm2.config.js file
module.exports = {
   "apps": [
     {
       "name": "postgraphile-mimic",
       "script": "postgraphile --append-plugins postgraphile-plugin-connection-filter -c postgres://localhost/mimic -s mimiciii -a -j -n 0.0.0.0 -p 15433 --enhance-graphiql --disable-query-log --no-ignore-indexes --simple-collections both",
       "env": {
         "PGUSER": "user",
         "PGPASSWORD": "password",
         "PGPORT": 15432,
       }
     },
     {
       "name": "postgraphile-fup",
       "script": "postgraphile --append-plugins postgraphile-plugin-connection-filter -c postgres://localhost/fup -s public -a -j -n 0.0.0.0 -p 15434 --enhance-graphiql --disable-query-log --no-ignore-indexes --simple-collections both",
       "env": {
         "PGUSER": "user",
         "PGPASSWORD": "password",
         "PGPORT": 15432,
       }
     }
   ]
 }

Launch the apps as below:

pm2 start pm2.config.js

Now use the command pm2 list to observe the status of the launched applications.

If you are interested in gaining experience in working on cutting edge technologies Apply here.

Leave a Reply