Reputation: 423
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!
Upvotes: 42
Views: 83614
Reputation: 412
My fix was silly admittedly, but I was missing the NODE_ENV=development value in my .env file. but was checking for it in my db config object.
Upvotes: 1
Reputation: 136
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();
Upvotes: 6
Reputation: 1430
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)
Upvotes: 3
Reputation: 942
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.
Upvotes: 13
Reputation: 231
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.
Upvotes: 1
Reputation: 1026
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.
Upvotes: 12
Reputation: 139
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.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).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
}
});
Upvotes: 3
Reputation: 327
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
Upvotes: 1
Reputation: 14823
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.
Upvotes: 22