问题
I am trying to connect to my Heroku PostgreSQL DB and I keep getting an SSL error. Does anyone have an idea on how to enable SSL in the connection string?
postgres://user:pass@host:port/database;
Been looking for it everywhere but it does not seem to be a very popular topic. By the way, I am running Nodejs and the node-pg module with its connection-pooled method:
pg.connect(connString, function(err, client, done) {
/// Should work.
});
Comments are much appreciated.
回答1:
You can achieve this like this:
postgres://user:pass@host:port/database?ssl=true
回答2:
You also can use this code below when create a new Client from node-postgres:
var pg = require("pg");
var client = new pg.Client({
user: "yourUser",
password: "yourPass",
database: "yourDatabase",
port: 5432,
host: "host.com",
ssl: true
});
client.connect();
var query = client.query('CREATE TABLE people(id SERIAL PRIMARY KEY, name VARCHAR(100) not null)');
query.on('row', function(row) {
console.log(row.name);
});
query.on('end', client.end.bind(client));
Hope this helps!
回答3:
With Google Cloud PG and pg-promise I had a similar need.
The error I got (using ?ssl=true
) was connection requires a valid client certificate
.
SSL connection is not documented for pg-promise
but it is built on node-postgres. As explained in the link, the ssl
config parameter can be more than just true
:
const pgp = require('pg-promise')();
const fs = require('fs');
const connectionConf = {
host: 'myhost.com',
port: 5432,
database: 'specific_db_name',
user: 'my_App_user',
password: 'aSecretePass',
ssl: {
rejectUnauthorized : false,
ca : fs.readFileSync("server-ca.pem").toString(),
key : fs.readFileSync("client-key.pem").toString(),
cert : fs.readFileSync("client-cert.pem").toString(),
}
};
const new_db = pgp(connectionConf);
new_db.any('SELECT * FROM interesting_table_a LIMIT 10')
.then(res => {console.log(res);})
.catch(err => {console.error(err);})
.then(() => {new_db.$pool.end()});
回答4:
You can also use environment variables to set up the connection. Here is an example.
(Assuming you have a Postgres DB running on port 5432@localhost and the DB supports SSL connection)
.env
PGHOST=localhost
PGPORT=5432
PGDATABASE=mydb
PGUSER=pguser1
PGPASSWORD=mypassword
PGSSLMODE=require
(Ensure you set PGSSLMODE
to require
as shown above.)
db.js
require('dotenv').config()
const { Pool } = require('pg')
// pools will use environment variables for connection information
const pool = new Pool()
// const pool = new Pool({ ssl: true }); This works too in the absence of PGSSLMODE
pool.on('error', function (err) {
console.log('idle client error', err.message, err.stack)
})
module.exports = {
pool,
query: (text, params, callback) => {
return pool.query(text, params, callback)
}
}
server.js
const express = require('express')
const { pool } = require('./db')
const app = express()
const port = 3000
app.get('/', async (req, res) => {
console.log('Request received...')
const result = await pool.query(`SELECT * FROM organization`);
res.send(result)
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Note: in case your Postgres database does not support SSL connections, you will have the following error when your application tries to make a query:
Error: The server does not support SSL connections
at Socket.<anonymous> (node_modules/pg/lib/connection.js:87:35)
References:
- https://www.postgresql.org/docs/9.6/libpq-connect.html#LIBPQ-CONNECT-SSLMODE
回答5:
For anybody looking for a TypeORM solution, it's also {ssl: true}
.
Full example:
const connectionOptions: PostgresConnectionOptions = {
name: `default`,
type: `postgres`,
url: process.env.DATABASE_URL,
ssl: process.env.DATABASE_SSL === `true`
}
来源:https://stackoverflow.com/questions/22301722/ssl-for-postgresql-connection-nodejs