31

I am following Heroku's node.js tutorial to provision a Postgres database. After creating a simple table and connecting to localhost:5000/db, I get an error saying "Error: The server does not support SSL connections".

I've been searching for solutions for hours but can't seem to fix it. Your help will be greatly appreciated. Thank you!

1
  • Did you set ssl = on in postgresql.conf, provide server certificates and restart the server? Jan 22, 2019 at 6:49

8 Answers 8

15

Here I provide a workaround for the question, and not the title of this post. A better answer to the post overall would probably address configuring SSL for the local machine.

I arrived here trying to resolve the problem of finishing that Heroku tutorial mentioned in the question and setting up the Postgres database to work locally as well as remotely.

const pool = new Pool({
    connectionString: process.env.DATABASE_URL || 'postgresql://postgres:<your admin password>@localhost:5432/<your db name>',
    ssl: process.env.DATABASE_URL ? true : false
})

My idea is to use SSL on the app I deploy but dodge SSL altogether on the local machine. By simply skipping SSL config on the local machine I am able to concentrate my efforts on developing a working app that still uses Heroku's built in SSL.

I use the Heroku environment variables to detect their environment versus my own and I select values accordingly in the code sample above. For me this has worked both locally and remotely.

10

This is the new form Heroku is working today in 2021, they made small corrections in the connection.

const pool = (() => {
if (process.env.NODE_ENV !== 'production') {
    return new Pool({
        connectionString: process.env.DATABASE_URL,
        ssl: false
    });
} else {
    return new Pool({
        connectionString: process.env.DATABASE_URL,
        ssl: {
            rejectUnauthorized: false
          }
    });
} })();

They changed a bit, using this way you can keep local and heroku available.

5

This helped me, when I was using connectionUrl.

postgres://password:postgres@localhost:5432/database?sslmode=disable

You might have noticed I have added ?sslmode=disable at the end of connection url.

For more information about sslmode check this out.

2

I had the same issue for setting up my local environment after the tutorial. Sticking to the command heroku local to start my server fixed it for me. This command is detailed in the Run the app locally section of the tutorial.

In my case I added a Express server and a client side using React + Webpack. My package.json scripts for local development look like:

"dev": "run-p dev:*",
"dev:server": "heroku local -f Procfile.dev",
"dev:webpack": "webpack --watch --config webpack.dev.js --mode development",
  • run-p is just from npm-run-all to run all scripts starting with dev at the same time.
  • The server uses the heroku local command and a specific Procfile for local development (use -f flag to do this), where I start a server locally with nodemon to watch for changes (see the Procfile.dev below).
  • The webpack script builds and watches for changes on the client side with React.

Procfile.dev for local development:

web: nodemon index.js

Procfile for production:

web: node --optimize_for_size --max_old_space_size=920 --gc_interval=100 index.js

Then the same code for connecting to the DB from the Provision a database section works for me:

const { Pool } = require('pg');
const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  ssl: {
    rejectUnauthorized: false
  }
});
1
  • 1
    If this doesn't work, also check that the connectionString that your are passing to the Pool instance is correct (and not undefined), this would trigger the same error. Feb 6, 2021 at 11:44
1

I came here not looking for a localhost solution (I wanted to connect to my db which is online), however, this turned up as the first google result, so I'll add a solution for not localhost issues here:

Check out the .env file to duplicate the Environment variables you setup in your heroku environment; see here: https://devcenter.heroku.com/articles/heroku-local#set-up-your-local-environment-variables

1

const pool = new Pool({ connectionString: process.env.DATABASE_URL || 'postgresql://postgres:@localhost:5432/', ssl: process.env.DATABASE_URL ? true : false })

In your code, you add this snippet with the credentials and connection string details, here process.env.DATABASE_URL comes from environment file, if it is there as it will enable ssl mode, else in local without ssl it works. Make sure, you will mention env variable only for other than local. So on both environment it works.

1
  • 1
    Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.
    – Community Bot
    Sep 2, 2021 at 7:34
1

To fix this I had to edit the database.js file. Path to file:

<path-to-your-strapi-project>/config/database.js

if you find something with ssl that is set to true set it to false. In my case, I used Mysql it looked like this:

ssl: env.bool('DATABASE_SSL', true)

Change it to:

ssl: env.bool('DATABASE_SSL', false)

0

Best solution that I found that worked only:

const client = new Client({
    connectionString: productiondbLink || localdblink,
    ssl: process.env.DATABASE_URL ? { rejectUnauthorized: false } : false
});

client.connect();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.