Skip to content

Instantly share code, notes, and snippets.

@bookcasey
Last active September 25, 2021 19:38
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save bookcasey/eb090fd0b6c17302b1bc1d0fd714c8d4 to your computer and use it in GitHub Desktop.
Save bookcasey/eb090fd0b6c17302b1bc1d0fd714c8d4 to your computer and use it in GitHub Desktop.
How to fix Node/pg version compatibility issues, and set the correct SSL options for heroku

How to fix common Node/pg compatability issues locally and on Heroku

Certain versions of the pg library do not play nice with certain versions of node.

The following combinations are known to work:

  • Node v12 and pg@7
  • Node >=v14 and pg@8

Additionally, different versions of pg on heroku requires certain ssl options to be set certain ways.

Steps to fix

If you're having issues with migrating the database or connecting to the database with your app locally or remotely, try the following steps:

  1. Run the command node -v then find the related section for your local version of node

If you have Node v12 installed locally

  1. In package.json in your dependencies set pg: ^7.18.2

  2. In package.json add or update the following lines:

"engines": {
  "node": "12.x"
}

This tells Heroku what version of node to use, and we chose the same version you are running locally.

  1. Add the following lines to the top of postgrator-config.js and server.js
const pg = require('pg');
pg.defaults.ssl = process.env.NODE_ENV === "production";

This will set the correct SSL flag for heroku, without breaking your app for local development

Note: Make sure that you don't have the SSL options set another way, such as ssl: true in your postgrator options

If you have Node v14 or above installed locally

  1. In package.json in your dependencies set pg: ^8.5.1

  2. In package.json add or update the following lines:

"engines": {
  "node": "14.x"
}

This tells Heroku what version of node to use, and we chose the same version you are running locally.

  1. Add the following lines to the top of postgrator-config.js and server.js
const pg = require('pg');
pg.defaults.ssl = process.env.NODE_ENV === "production" ? { rejectUnauthorized: false } : false;

This will set the correct SSL flag for heroku, without breaking your app for local development

Note: Make sure that you don't have the SSL options set another way, such as ssl: true in your postgrator options

@artificialarea
Copy link

THANK YOU OH SO MUCH!!! I was at my wit's end until someone shared this with me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment