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
I think you should use
db
(The name of database service name) as yourDATABASE_HOST
to connect successfully.Your
docker-compose.yml
only defines the database. It exposes port5432
locally, meaninglocalhost: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.I had the same problem with Strapi and PostgreSQL. I have managed to get it working by changing the connection property from
username
touser
.Here is an example of how my config/database.js file looks like :