问题
I am following this article ((http://nodeexamples.com/2012/09/21/connecting-to-a-postgresql-database-from-node-js-using-the-pg-module/). I have already deployed my app to heroku and currently using express, node.js, to try and connect to a PostgresSQL database in Heroku that I just installed. I get to the very end of the article and I use the command
node myfile.js
I get this error
error: no pg_hba.conf entry for host "...", user "...", database "...", ...
How do I go about creating one and where in my app directory should I put it?
Below is the entire error message. I changed the strings for IP address, user, and database but it looks basically just like it.
events.js:72
throw er; // Unhandled 'error' event
^
error: no pg_hba.conf entry for host "00.000.000.00", user "username", database "databasename", SSL off
at Connection.parseE (/Users/user/workspace/MyApp/app/node_modules/pg/lib/connection.js:526:11)
at Connection.parseMessage (/Users/user/workspace/MyApp/app/node_modules/pg/lib/connection.js:356:17)
at Socket.<anonymous> (/Users/user/workspace/MyApp/app/node_modules/pg/lib/connection.js:105:22)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:748:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:410:10)
at emitReadable (_stream_readable.js:406:5)
at readableAddChunk (_stream_readable.js:168:9)
at Socket.Readable.push (_stream_readable.js:130:10)
Edit: I did some more research and found that the 'pg_hba.conf' file is in my
/usr/local/var/postgres
and I added this line into the 'pg_hba.conf' file
# TYPE DATABASE USER ADDRESS METHOD
host all all trust
also tried
# TYPE DATABASE USER ADDRESS METHOD
host all all 0.0.0.0/0 md5
but it keeps saying there is no entry for my host, user, database, etc... is my 'pg_hba.conf' syntax wrong in any way?
回答1:
Change your connection code to use ssl. Following your linked example:
var conString = "pg://admin:guest@localhost:5432/Employees";
var client = new pg.Client(conString);
client.connect();
becomes:
var client = new pg.Client({
user: "admin",
password: "guest",
database: "Employees",
port: 5432,
host: "localhost",
ssl: true
});
client.connect();
https://github.com/brianc/node-postgres/wiki/Client#new-clientobject-config--client
回答2:
In the case sequelize ignores all of your efforts to turn ssl on, you can try to convince pg to enable ssl for all conncetions by default:
var pg = require('pg');
pg.defaults.ssl = true;
const Sequelize = require('sequelize');
const sequelize = new Sequelize('postgres://...');
回答3:
We ran into this error while upgrading the pg database on heroku from hobby tier to standard-0. SSL is required, but we didnt set it in our config.
Include in config when initialize new Sequelize(...)
"dialect": "postgres",
"dialectOptions": {
"ssl": true
}
This trick was, that the ssl option is wrapped in dialectOptions.
found here: https://github.com/sequelize/sequelize/issues/956#issuecomment-147745033
回答4:
You can also use the ENVIRONMENT CONFIG VARIABLE 'PGSSLMODE' to 'require' via Heroku's web interface or CLI.
Case: Postgres dB set up as a Heroku add-on and attached to app on a Heroku Dyno.
Heroku provides some pretty good support on how to connect to one of its add-on databases; however, it unfortunately leaves out (or, I missed it) any mention of what do to enforce SSL since all Heroku dB tiers starting with Standard-0 enforces SSL by default.
回答5:
What worked for me was a combination of above answers and a comment(from @schybo)
let cloud_config = {
username: process.env.DB_USERNAME,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOSTNAME,
port: 5432,
ssl: true,
dialect: 'postgres',
dialectOptions: {
"ssl": {"require":true }
}
};
Use both ssl: true, and dialectOptions: { "ssl": {"require":true }}
Comment on Sequelize issue which is also added to the docs.
回答6:
Just add a flag to the client initialisation:
Change
const conString = "pg://admin:guest@localhost:5432/Employees"
const client = new pg.Client(conString);
client.connect();
To
const conString = "pg://admin:guest@localhost:5432/Employees"
const client = new pg.Client({
connectionString: process.env.DATABASE_URL,
ssl: true
});
client.connect();
回答7:
setting ssl: true worked for me
let pg = require('pg');
let pgClient = new pg.Client({
user: "admin",
password: "guest",
database: "Employees",
port: 5432,
host: "localhost",
ssl: true
});
pgClient.connect();
来源:https://stackoverflow.com/questions/25000183/node-js-postgresql-error-no-pg-hba-conf-entry-for-host