PostgreSQL error when trying to connect from node application

12/30/2018
FATAL: no PostgreSQL user name specified in startup packet

Logs from my postgreSQL instance in my kubernetes cluster when trying to connect to it by doing the following:

const { POSTGRES_DATABASE, POSTGRES_USERNAME, POSTGRES_PASSWORD } = require('../config/config');

const Sequelize = require('sequelize');
    const conn = new Sequelize(POSTGRES_DATABASE, {
        username: POSTGRES_USERNAME,
        password: POSTGRES_PASSWORD
    });

config/config.js

    const config = {
        POSTGRES_DATABASE: 'postgres://postgres/postgresdb',
        POSTGRES_USERNAME: 'postgresadmin',
        POSTGRES_PASSWORD: 'admin123',
        SERVER_PORT: '5000'
    }

module.exports = config;

I am using SequelizeJS in nodeJS. http://docs.sequelizejs.com/

It seems like the requests are connecting alright due to seeing the attempts in the postgreSQL logs. However something goes wrong and I wonder if it's wrong with permissions in postgres or the node service.

Appreciate some help or ideas

-- Joelgullander
kubernetes
node.js
postgresql
sequelize.js

1 Answer

12/30/2018

According to the Sequelize documentation that you linked, the args to creating a new connection are db, username, password. Your code did db, and then an object with username and password keys. This object is not correct and caused it to not find the username or password. Try this instead:

const { POSTGRES_DATABASE, POSTGRES_USERNAME, POSTGRES_PASSWORD } = require('../config/config');

const Sequelize = require('sequelize');
const conn = new Sequelize(POSTGRES_DATABASE, POSTGRES_USERNAME, POSTGRES_PASSWORD);

Sequelize does allow for an optional options object as the last argument, but the username and password are not expected to be in there.

This page of the documentation feels confusing to me.

http://docs.sequelizejs.com/manual/installation/usage.html

I believe what it says is that you either need to define everything in the object, or do the database, username, and password as separate arguments, but I don’t see it supporting database in the arguments and username / password in the object.

It seems like this should work as well:

const conn = new Sequelize({
  database: POSTGRES_DATABASE,
  username: POSTGRES_USERNAME,
  password: POSTGRES_PASSWORD
});
-- Nate
Source: StackOverflow