0
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
  • 1,624
  • 2
  • 20
  • 46
  • is `POSTGRES_USERNAME` a variable? Where is it being defined? – doublesharp Dec 30 '18 at 14:55
  • I updated first post @doublesharp – Joelgullander Dec 30 '18 at 14:57
  • https://stackoverflow.com/questions/18630323/no-postgresql-user-name-specified-in-startup-packet could you refer that one? – PPShein Dec 30 '18 at 15:44
  • I am not connecting with username / password in the URI. @PyaePhyoeShein – Joelgullander Dec 30 '18 at 15:47
  • 2
    You are passing in the username for the password - `password: POSTGRES_USERNAME` – doublesharp Dec 30 '18 at 15:48
  • And not passing in the port – doublesharp Dec 30 '18 at 15:48
  • The default Postgres port is 5432. If you’re planning to use 5000, you need to pass that in to your connection. Right now you’re extracting 3/4 of your variables from your config and only using 2 of them. Also, what ip address is this server located? It’s going to expect the host to be localhost unless you tell it otherwise. If it’s on localhost then you’re fine, but if not then you should configure that as well. – Nate Dec 30 '18 at 15:51
  • @doublesharp port shouldn't be needed, the SERVER_PORT is meant for which port the node app should run on. I changed to POSTGRES_PASSWORD but still getting same error – Joelgullander Dec 30 '18 at 15:53
  • 1
    Also, the database you’re using differs from the one in the configure. That’s seems odd. – Nate Dec 30 '18 at 15:53
  • postgres.default is the IP to the service. can be called with both .default and without. The thing is it reaches the postgres service, but still failing on FATAL no username thing – Joelgullander Dec 30 '18 at 15:54
  • 1
    I guess my point is, either use the one from your config.js, or delete it from config.js. Adding it to config.js and not using it is confusing and will lead to more confusion later. – Nate Dec 30 '18 at 15:55
  • Agreed, Changed it. – Joelgullander Dec 30 '18 at 15:56
  • @Nate the internal dns to the postgres is tcp://postgres – Joelgullander Dec 30 '18 at 15:59

1 Answers1

3

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
  • 2,364
  • 1
  • 10
  • 16
  • Thanks buddy, nice catch. Now I get Error: connect ECONNREFUSED 127.0.0.1:5432 And it's not localhost I want to connect to, so I guess i'll need to define just the database name in the POSTGRES_DATABASE and then set a host somewhere in the options object – Joelgullander Dec 30 '18 at 16:11
  • You need to add a`host` to the options object then. – Nate Dec 30 '18 at 16:12
  • I now realize that my last comment ran together. A “host” key is what you need to specify, for it to know the IP address or domain of the database. It looks like you’ll also want to set dialect to 'postgres'. I’m uncertain if Sequelize is aware that Postgres runs on port 5432 by default. You may need to specify that as the “port” option as well. – Nate Dec 30 '18 at 16:22
  • I'm currently doing dialect: 'postgres', host: 'postgres' but it seems it doesn't reach the postgres service now at all. it says port is based of dialect so shouldnt be needed. // custom port; default: dialect default – Joelgullander Dec 30 '18 at 16:25
  • Is node running directly on your computer? If you’re running it in a Docker container or a Virtual Machine, you may be having a problem with DNS not being shared from your computer to the VM. – Nate Dec 30 '18 at 16:28
  • Also, silly question but it’s good to double check — Is the Postgres actually running? – Nate Dec 30 '18 at 16:32
  • No I have the node & postgres deployed in a kubernetes cluster and I know the internal DNS works due to having other services running and communicating between eachother. – Joelgullander Dec 30 '18 at 16:37
  • Like you said it was a kubernetes issue at last. everything is running now with the changes you suggested! Thanks for all the help – Joelgullander Dec 31 '18 at 01:20
  • Glad to hear it! No problem. – Nate Dec 31 '18 at 01:29