I have a dockerized PSQL instance and a dockerized app that connects to it. I am getting an incorrect password error trying to connect despite never setting one. Default of postgres doesn’t work.
compose.yml
services:
psql:
build: database/.
env_file: vars.env
ports:
- "5432:5432"
reader:
build: reader/.
env_file: vars.env
restart: always
reader.py
import psycopg2
connection = psycopg2.connect(
database="postgres",
user="postgres",
password="postgres",
host="psql",
port=5432)
cursor = connection.cursor()
Error
psycopg2.OperationalError: connection to server at "psql" (172.29.0.3), port 5432 failed: FATAL: password authentication failed for user "postgres"
What could this password be? Or how is it set. I thought no default user or pass was set.
>Solution :
You have to make sure the vars.env file has these:
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
For trouble shooting I’d remove the vars.env and uses a direct environment variable as the docs suggests, to see if it works:
psql:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: postgres
From docs:
The PostgreSQL image uses several environment variables which are easy
to miss. The only variable required is POSTGRES_PASSWORD, the rest are
optional.