0

I am developping a backend application with node and sequelize. My database is from postgresql.

When lauching the app, the database connection works fine, but when it tries to communicate with the database to read or update, it fails with a connection error: password authentication failed for user "wushin".

Seems really weird to me because database connection has already been done, and password has been validated. Do you guys know what's happening ? Maybe an issue with pg module but I tried different versions.

Versions Node: 10.17.0 Sequelize: 5.21.3 Postgres: 10.11 pg module: 7.17.1

-> This code works fine:

const sequelize = new Sequelize(process.env.DATABASE_DEV_URL)

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.\n')
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err)
  })

-> But this promise fails with SequelizeConnectionError:

models.Question.findAll()
    .then(data => {
      console.log('-> Succeeded data fetching\n')
      console.log(data)
    })
    .catch(err => {
      console.log('-> Failed data fetching\n')
      console.log('Error', err)
    })

Logs:

yarn run v1.19.2
$ node index.js
Example app listening on port 4000 or something!
Executing (default): SELECT 1+1 AS result
Connection has been established successfully.

- Trying to fetch data:
-> Failed data fetching
Error:
 { SequelizeConnectionError: password authentication failed for user "wushin"
    at connection.connect.err (/home/wushin/Projects/GuessGame/theguessgame-api/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:182:24)
    at Connection.connectingErrorHandler (/home/wushin/Projects/GuessGame/theguessgame-api/node_modules/pg/lib/client.js:194:14)
    at Connection.emit (events.js:198:13)
    at Socket.<anonymous> (/home/wushin/Projects/GuessGame/theguessgame-api/node_modules/pg/lib/connection.js:128:12)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:287:12)
    at readableAddChunk (_stream_readable.js:268:11)
    at Socket.Readable.push (_stream_readable.js:223:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  name: 'SequelizeConnectionError'
2
  • can you show your DATABASE_DEV_URL, just obscure your password... also post the code where you associate your models with the sequelize instance you established the connection with Commented Jan 12, 2020 at 7:13
  • If you're using node.js dotenv and pg make sure you have require('dotenv').config(); in your index.js. This fixed this error once I added it. Commented Jan 22, 2022 at 9:05

2 Answers 2

2

It seems that you pass no configurations to Sequelize but the host. The minimum configurations are host, port, databasename, dialect username, and password.

From the docs:

 const Sequelize = require('sequelize');

 // Option 1: Passing parameters separately const sequelize = new
 Sequelize('database', 'username', 'password', {   
   host: 'localhost',  
   dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */ 
 });

// Option 2: Passing a connection URI const sequelize = new
 Sequelize('postgres://user:[email protected]:5432/dbname');
Sign up to request clarification or add additional context in comments.

Comments

0

I finally fixed this. The issue was that with sequelize, requiring the models calls an index.js that is supposed to do the sequelize connection for you, using the config repository sequelize creates.

My connection to sequelize was working well but the one that was launched by requiring models had some bad information on my database.

Therefore I could not use the imported model to fetch data on the database.

I inserted good config information :

require('dotenv').config()

module.exports = {
  development: {
    url: process.env.DATABASE_URL,
    dialect: 'postgres',
  },
  test: {
    url: process.env.DATABASE_TEST_URL,
    dialect: 'postgres',
  },
  production: {
    url: process.env.DATABASE_PROD_URL,
    dialect: 'postgres',
  },
}

And completely removed the line that I wrote myself:

const sequelize = new Sequelize(process.env.DATABASE_DEV_URL)

It is now the models/index.js that connects to the database with :

const sequelize = new Sequelize(process.env.DATABASE_URL)

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.\n')
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err)
  })

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.