skip to Main Content

I am trying to connect Strapi with PostgreSQL database. I am also using Docker in order to compose the "db". I have managed to make the PostgreSQL running on port 5432, and I am also able to connect to the database with docker exec -it nameOfContainer bash command without any problems. The only problem I do have is that I can not connect to the database from Strapi. I keep getting error password authentication failed for user "strapibase". I know that the username of superuser is "strapibase" but I keep getting errors on and can not run the Strapi yarn run develop command.

docker-compose.yml

version: '3.3'

services:
  db:
    image: postgres:alpine
    networks:
      - strapi-app-network
    volumes:
      - db-data:/data
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: strapibase
      POSTGRES_USER: strapibase
      POSTGRES_PASSWORD: strapibase

volumes:
  db-data:

networks:
  strapi-app-network:
    driver: bridge

database.js

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connections: {
    default: {
      connector: "bookshelf",
      settings: {
        client: "postgres",
        host: env("DATABASE_HOST", "localhost"),
        port: env.int("DATABASE_PORT", 5432),
        database: env("DATABASE_NAME", "strapibase"),
        username: env("DATABASE_USERNAME", "strapibase"),
        password: env("DATABASE_PASSWORD", "strapibase"),
        schema: env("DATABASE_SCHEMA", "public"),
      },
      options: {},
    },
  },
});

Can someone tell me what am I doing wrong here?

docker ps -a

CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                    NAMES
94873c9919bc   postgres:alpine   "docker-entrypoint.sā€¦"   33 minutes ago   Up 26 minutes   0.0.0.0:5432->5432/tcp   strapi-backend-db-1

I do have pgAdmin4 also installed on desktop, tried adding server there also keep getting the same error

3

Answers


  1. I think you should use db (The name of database service name) as your DATABASE_HOST to connect successfully.

    Login or Signup to reply.
  2. Your docker-compose.yml only defines the database. It exposes port 5432 locally, meaning localhost:5432 is the right hostname in this case.

    Verify the connection locally by running psql -h 127.0.0.1 -U strapibase -d strapibase and enter the password.

    Login or Signup to reply.
  3. I had the same problem with Strapi and PostgreSQL. I have managed to get it working by changing the connection property from username to user.

    Here is an example of how my config/database.js file looks like :

    module.exports = ({ env }) => ({
      defaultConnection: "default",
      connection: {
        client: "postgres",
        connection: {
          host: env("DATABASE_HOST", "localhost"),
          port: env.int("DATABASE_PORT", 5432),
          database: env("DATABASE_NAME", "strapibase"),
          user: env("DATABASE_USERNAME", "strapibase"), // <--- here from username to user
          password: env("DATABASE_PASSWORD", "strapibase"),
          schema: env("DATABASE_SCHEMA", "public"),
          ssl: env("DATABASE_SSL", false),
        },
      },
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search