Update Fri 8 Jun 2018 : I was running into CORS issues with the ApolloClient in React. So I enabled CORS with the –cors option while I’m still in development.

This is a multipart blog post about using PostGraphile. Part 1 is here.

This 2nd post only goes through the tricky steps required to connect an EC2 running PostGraphile to a RDS PostgreSQL instance.

These are:

  1. Do not forget to add a custom inbound rule to allow the ec2 to access the RDS instance (AWS Console).
  2. Make PostGraphile listen to your public ip address instead of localhost (Postgraphile CLI)
  3. Use pm2 to run in production

The experienced developer may stop here and follow the above instructions.

For others, follow me below for additional guidance.

Custom Inbound Rule for your RDS instance

This step involves adding an inbound rule to your RDS instance security group. Navigate to your security group in the RDS dashboard. Then make sure to add a PostgreSQL inbound rule, selecting the EC2 security group in the source textbox.

Screen Shot 2018-06-06 at 16.12.30

Listening to your EC2 public IP

Don’t be a fool like I use to be sometimes 🙂 PostGraphile listening on localhost won’t serve your api over the internet. You must make PostGraphile use your EC2’s public DNS name 😉

So make sure to use the –host option of the cli and you’ll be fine.

Using pm2

Basic Setup

First, we need the absolute path to npm’s globally installed modules by issuing the following command:

npm root -g

In my case it returned:


Next, we need an ecosystem.config.js file. Run:

pm2 ecosystem

Launch your favorite editor and make the modifications, as shown in the sample below.

module.exports = {
   * Application configuration section
   * http://pm2.keymetrics.io/docs/usage/application-declaration/
  apps : [

    // First application
      name	: 'postgraphile',
      script    : '/home/ec2-user/.nvm/versions/node/v8.11.2/bin/postgraphile',
      args	: '-c postgres://my-postgres-rds-instance.eu-west-1.rds.amazonaws.com/hygeex --watch --host ec2-my-ec2-ip-v4.eu-west-1.compute.amazonaws.com --cors',
      "instances": 1,
      "exec_mode": "cluster",
      "env": {
	"PGUSER": "theuser",
        "PGPASSWORD": "thepassword",
        "PGPORT": 5432,

The tricky part was to use some of the environment variables to not embed the values directly in the args parameters. Of course make the changes relative to your RDS instance and ec2 public host name.

Checking that all is well

You can now start your application:

pm2 start ecosystem.config.js

Check that PostGraphile is running by looking a the logs:

pm2 logs

Finally go to the graphiql endpoint to confirm that all the stars are aligned 🙂

Making it to run permanently

Simply run:

pm2 startup

And follow the instructions. Following is the output of the command on my ec2:

[PM2] Init System found: <b>systemd</b>[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/home/ec2-user/.nvm/versions/node/v8.11.2/bin /home/ec2-user/.nvm/versions/node/v8.11.2/lib/node_modules/pm2/bin/pm2 startup systemd -u ec2-user --hp /home/ec2-user

Final check

Reboot your ec2 instance.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.