'How to fix "Error: The server does not support SSL connections" when trying to access database in localhost?

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!



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

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
  }
});

Solution 4:[4]

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

Solution 5:[5]

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.

Solution 6:[6]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ThisClark
Solution 2 JBarros35
Solution 3
Solution 4 Human_BetaRelease
Solution 5
Solution 6 Ali Hussnain