4

I have some of a database set up with PostgreSQL, and I am able to do everything I need from the psql REPL sort of thing, but when I try to access this though node.js on the same machine I get an password authentication filed for user"[my user name]" error.

As per an online tutorial, my database acces code is something like this:

var pg = require('pg');
var path = require('path');
var connectionString = require(path.join(__dirname, '../', '../', 'config'));

var client = new pg.Client(connectionString);
client.connect();
var query = client.query('CREATE TABLE items(id SERIAL PRIMARY KEY, text VARCHAR(40) not null, complete BOOLEAN)');
query.on('end', function() { client.end(); });

But as I already have the tables set up with some of my own functions, I'm simply trying to access those functions on POSTs, with:

var express = require('express');
var router =  express.Router();
var pg = require('pg');
var path = require('path');
var connectionString = 'postgres://localhost:5432/[My User Name]?ssl=true;

router.post('/locations', function(req,res) {
    var client = new pg.Client(connectionString);
    client.connect();

    var query = client.query([Call to my function, works in REPL, something like "SELECT * FROM create_location([Data from req])"]);
});

I have my pg_.conf set up as:

local  all  postgres         peer
local  all  all              trust
local  all  all 127.0.0.1/32 trust
host   all  all ::1/128      md5

1 Answer 1

9

your connectionstring seems to be wrong. a connection string has to be like:

postgres://[username]:[password]@[host]:[port]/[databasename]

and in your case:

postgres://[username]@localhost:5432/[databasename]?ssl=true
Sign up to request clarification or add additional context in comments.

4 Comments

In that case, would I be required to always have my password hanging around in plaintext?
if your pg_config is set up to trust all local users, you can remove the passwort part from the connection string. but the last argument has to be the databasename and not the username
I believe my database name and user name are the same
Note that when the user isn't specified, defaults are used.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.