Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot connect to Postgres database with Sequelize using docker-compose

I can’t manage to authenticate my postgres database. I am using docker-compose. Please find below the relevant files and logs. Is something about my connection string wrong?

docker-compose.yml

version: "3.8"
services:
  app:
    build:
      context: ./
      target: dev
    volumes:
      - .:/src
    command: npm run start:dev
    ports:
      - "3000:3000"
    depends_on:
      - postgres
    environment:
      DATABASE_URL: postgres://votetracker@postgres/codes
      POSTGRES_PASSWORD: password
      NODE_ENV: development
      DEBUG: nodejs-docker-express:*
  postgres:
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: codes

db.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const { Sequelize } = require("sequelize");
const sequelize = new Sequelize("postgres://user:pass@database:5432/codes");

const authenticate = async () => {
  try {
    await sequelize.authenticate();
    console.log("Connection has been established successfully.");
  } catch (error) {
    console.error("Unable to connect to the database:", error);
  }
};
module.exports = { authenticate };

When running docker-compose up, this is the output:

postgres_1  | The files belonging to this database system will be owned by user "postgres".
postgres_1  | This user must also own the server process.
...
postgres_1  | Success. You can now start the database server using:
postgres_1  |
postgres_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
...
postgres_1  | server started
postgres_1  | CREATE DATABASE
...
postgres_1  | PostgreSQL init process complete; ready for start up.
...
postgres_1  | 2022-01-23 17:32:03.824 UTC [1] LOG:  database system is ready to accept connections
...
app_1       | Your app is running on port 3000
app_1       | Unable to connect to the database: ConnectionError [SequelizeConnectionError]: getaddrinfo EAI_AGAIN database

>Solution :

You’re almost there. When you need to connect to another container on a docker-compose bridge network, you use the service name as the hostname.

So instead of

const sequelize = new Sequelize("postgres://user:pass@database:5432/codes");

you need

const sequelize = new Sequelize("postgres://user:pass@postgres:5432/codes");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading