When I try to start a container like this:
db:
container_name: database-pg
image: postgres
ports:
- 127.0.0.1:5432:5432
restart: always
logging:
<<: *logging_default
volumes:
- datavolume:/var/lib/postgresql/data
- ./backend/db/migrations/0001-init.up.sql:/docker-entrypoint-initdb.d/0001-init.up.sql
- ./envs/ropsten/seed.sql:/docker-entrypoint-initdb.d/0002-seed.sql
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_HOST_AUTH_METHOD=trust
I see this:
2021-01-09 12:00:48.362 UTC [89] FATAL: password authentication failed for user "postgres"
2021-01-09 12:00:48.362 UTC [89] DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 99: "host all all all md5"
2021-01-09 12:01:47.477 UTC [91] FATAL: password authentication failed for user "postgres"
2021-01-09 12:01:47.477 UTC [91] DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 99: "host all all all md5"
2021-01-09 12:01:47.909 UTC [92] FATAL: password authentication failed for user "postgres"
2021-01-09 12:01:47.909 UTC [92] DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 99: "host all all all md5"
2021-01-09 12:01:47.988 UTC [93] FATAL: password authentication failed for user "postgres"
2021-01-09 12:01:47.988 UTC [93] DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 99: "host all all all md5"
2021-01-09 12:01:48.540 UTC [94] FATAL: password authentication failed for user "postgres"
2021-01-09 12:01:48.540 UTC [94] DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 99: "host all all all md5"
2021-01-09 12:01:48.627 UTC [95] FATAL: password authentication failed for user "postgres"
2021-01-09 12:01:48.627 UTC [95] DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 99: "host all all all md5"
I cannot understand why this is happening. Why does postgres show this error at all, what else should the password be at startup.
I've tried to run postgres using your configuration (I've just removed the logging and volumes section) and everything was fine.
It seems to me that you put the POSTGRES_HOST_AUTH_METHOD=trust config after initializing the database. There is a note in the official docker image readme:
This optional variable can be used to control the auth-method for host connections for all databases, all users, and all addresses. If unspecified then md5 password authentication is used. On an uninitialized database, this will populate pg_hba.conf via this approximate line:
echo "host all all all $POSTGRES_HOST_AUTH_METHOD" >> pg_hba.conf
So, to fix your problem you should either delete your data and recreate the database or mount your own updated pg_hba.conf file:
version: '3'
services:
db:
container_name: database-pg
image: postgres
ports:
- 127.0.0.1:5433:5432
restart: always
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_HOST_AUTH_METHOD=trust
command: postgres -c 'hba_file=/var/lib/postgresql/pg_hba.conf'
volumes:
- datavolume:/var/lib/postgresql/data
- ./backend/db/migrations/0001-init.up.sql:/docker-entrypoint-initdb.d/0001-init.up.sql
- ./envs/ropsten/seed.sql:/docker-entrypoint-initdb.d/0002-seed.sql
- ./hba.conf:/var/lib/postgresql/pg_hba.conf
where hba.conf:
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
# warning trust is enabled for all connections
# see https://www.postgresql.org/docs/12/auth-trust.html
host all all all trust
Related
So I have my strapi server, which should connect to the Docker container running my Postgres DB, but I get this error when I try to start the development server for the strapi component of my application.
no pg_hba.conf entry for host "127.0.0.1", user "admin", database "store", no encryption
error: no pg_hba.conf entry for host "127.0.0.1", user "admin", database "store", no encryption
This is what is within my pg_hba.conf file on my local
# "local" is for Unix domain socket connections only
local all all scram-sha-256
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all scram-sha-256
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
I also have a docker-compose.yml file which creates the postgres database, so I can connect the strapi server to it.
version: '3'
services:
database:
image: 'postgres'
container_name: nndesign
build:
context: .
target: nndesign
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: nuzhat
POSTGRES_DB: store
expose:
- 5432
ports:
- 5432:5432
volumes:
- ./scripts/postgres-data:/var/lib/postgresql/data
- ./scripts/admin_users.sql:/docker-entrypoint-initdb.d/admin_users.sql
This is what I have as environment variables so the strapi server can connect to the postgres db running in the docker container
export default ({ env }) => ({
connection: {
client: 'postgres',
connection: {
host: env('DATABASE_HOST', 'localhost'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'store'),
user: env('DATABASE_USERNAME', 'admin'),
password: env('DATABASE_PASSWORD', 'nuzhat'),
ssl: env.bool('DATABASE_SSL', false),
},
},
});
I'm a bit confused on how to revolve this error. Been trying to Google and also use ChatGPT to find a solution, but to no avail.
Any help would be greatly appreciated.
Thank you.
My attempt to start a PostgreSQL Docker container fails when doing docker-compose. I can connect to it if I do docker run instead. I don't know what the difference is. My intent with the compose is to have it create a database usable for development and that I can connect to with user postgres.
docker-compose.yml:
version: '3.7'
services:
my_app:
build:
dockerfile: ./dockerfile-my-app
context: .
command: tail -f /dev/null
depends_on:
- db
env_file: ./.env.development
ports:
- "8080:8080"
volumes:
- /c/Users/woodsman/linux-mint-woodsman/home/dev:/home/dev
db:
image: postgres:latest
env_file: ./.env.development
environment:
- POSTGRES_USER= postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=helloworld
- PGDATA=/var/lib/postgresql/data/helloworld2/
ports:
- "5432:5432"
restart: "no"
volumes:
- /c/Users/woodsman/linux-mint-woodsman/dbdata:/var/lib/postgresql/data/pgdata
volumes:
dbdata:
Attempting to connect to jdbc:postgresql://localhost:5432/helloworld using user postgres and password postgres. Please pardon any security concerns you may perceive. This will be tightened for development, and done much more securely when deployed to production.
The reported error as reported by SquirrelSQL is:
hello-world: FATAL: password authentication failed for user "postgres"
class org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
The image hashcode is:
9dbc24674f25eb449df11179ed3717c47348fb3aa985ae14b3936d54c2c09dde
>docker logs c4c2
2022-05-16 05:28:50.560 UTC [1] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-05-16 05:28:50.560 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2022-05-16 05:28:50.560 UTC [1] LOG: listening on IPv6 address "::", port 5432
2022-05-16 05:28:50.564 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-05-16 05:28:50.571 UTC [63] LOG: database system was shut down at 2022-05-16 05:28:50 UTC
2022-05-16 05:28:50.579 UTC [1] LOG: database system is ready to accept connections
2022-05-16 05:29:17.184 UTC [70] FATAL: password authentication failed for user "postgres"
2022-05-16 05:29:17.184 UTC [70] DETAIL: Role "postgres" does not exist.
Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
2022-05-16 05:36:41.983 UTC [78] FATAL: password authentication failed for user "postgres"
2022-05-16 05:36:41.983 UTC [78] DETAIL: Role "postgres" does not exist.
Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
I hope for a Docker based answer. If there's some PostgreSQL command I need to do (or not do), please tell me how I can pass that through Docker.
Thanks,
Woodsman
Your main error is:
Role "postgres" does not exist.
It seems because whitespace here in docker-compose.yml:
POSTGRES_USER= postgres
In first start postgres was initialized, so when You change "POSTGRES_USER" in variable, role will not create. You can delete you volume and start docker-compose again.
Good day!
I have a task to delimit user access to databases
for example, that user_1 would have access only to the database of "oranges" and user_2 only to the database of "watermelons"
I try to do this through pg_hba.conf, but I can still connect to the watermelon database through user_1
MY pg_hba.conf
# PostgreSQL Client Authentication Configuration File
# ===================================================
#
# Refer to the "Client Authentication" section in the PostgreSQL
# documentation for a complete description of this file. A short
# synopsis follows.
#
# This file controls: which hosts are allowed to connect, how clients
# are
#...
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
# local all all trust
# IPv4 local connections:
# host all all 127.0.0.1/32 trust
# IPv6 local connections:
# host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
# local replication all trust
# host replication all 127.0.0.1/32 trust
# host replication all ::1/128 trust
# host all all all scram-sha-256
host oranges user_1 password
host watermelons user_2 password
MY DOCKERFILE
FROM postgres:14.1
RUN rm -f /var/lib/postgresql/data/pg_hba.conf
COPY ./pg_hba.conf /var/lib/postgresql/data/pg_hba.conf
ADD ./init.sql /docker-entrypoint-initdb.d/
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["postgres"]
MY DOCKER-COMPOSE
version: "3.9"
services:
#DATABASE_SERVER
postgres:
build: ./databases/postgres
hostname: postgres
environment:
POSTGRES_PASSWORD: root
PGDATA: /var/lib/postgresql/data
ports:
- 5432:5432
What am I doing wrong? I will be grateful for any comment!
I faced a problem when I try to use psql command with my docker-compose file on my local Ubuntu machine:
psql: error: FATAL: role "postgres" does not exist
I tried to use others solution like removing docker image, volume. psql -U postgres doesn't work for me either.
I try to use first docker-compose up, then docker exec -it database bash
There's my docker-compose file
services:
db:
container_name: postgres
image: postgres:13.3-alpine
restart: always
user: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=root
ports:
- "5432:5432"
volumes:
- ./data/db:/var/lib/postgresql/data
Maybe this string tells something?
postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
OUTPUT:
Attaching to postgres
postgres |
postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres |
postgres | 2021-08-02 17:29:10.426 UTC [1] LOG: starting PostgreSQL 13.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20210424) 10.3.1 20210424, 64-bit
postgres | 2021-08-02 17:29:10.426 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres | 2021-08-02 17:29:10.426 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres | 2021-08-02 17:29:10.429 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres | 2021-08-02 17:29:10.433 UTC [12] LOG: database system was shut down at 2021-08-02 17:22:17 UTC
postgres | 2021-08-02 17:29:10.438 UTC [1] LOG: database system is ready to accept connections
postgres | 2021-08-02 17:37:53.452 UTC [33] FATAL: role "postgres" does not exist
postgres | 2021-08-02 17:37:56.958 UTC [35] FATAL: role "user" does not exist
postgres | 2021-08-02 17:41:54.294 UTC [45] FATAL: role "postgres" does not exist```
First, you've set POSTGRES_USER to root, so you're going to have a root user instead of postgres user.
Second, if a database already exists, it doesn't matter what you set for POSTGRES_USER and POSTGRES_PASSWORD -- postgres will use whatever is in the database.
So you can either:
Delete the database (rm -rf data/db) and start over, or
Edit your pg_hba.conf so that you don't need a password
docker-compose exec web rake db:migrate db:create - I have above error: role "..." does not exist.
When I try:
docker-compose exec web rake db:drop
docker-compose exec web rake db:setup
it's working.
Actually I'm using the following docker-compose.yml file
version: '3.3'
services:
postgres:
container_name: postgres
image: postgres:latest
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
PGDATA: /var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
volumes:
- ./data/postgres/pgdata:/var/lib/postgresql/data/pgdata
I use also this .env file in the same directory of the docker-compose.yml file:
POSTGRES_USER=dbadm
POSTGRES_PASSWORD=dbpwd
POSTGRES_DB=db
Then I run a bash shell into container this way:
docker exec -ti postgres bash
And after this invoke the command:
psql -h postgres -U dbadm db
And I get the error:
psql: FATAL: password authentication failed for user "dbadm"
The strange fact is that if use the default image parameters:
psql -h postgres -U admin database
And insert the default password "password", it logs me in, and seems it's ignoring the environment variables.
What am I missing?
Additional logs from docker-compose up logs:
postgres | 2017-10-15 09:19:15.502 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres | 2017-10-15 09:19:15.502 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres | 2017-10-15 09:19:15.505 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres | 2017-10-15 09:19:15.524 UTC [22] LOG: database system was shut down at 2017-10-15 09:02:21 UTC
postgres | 2017-10-15 09:19:15.530 UTC [1] LOG: database system is ready to accept connections
Cannot see any "RUN" line about user, database and password setup.
according to https://hub.docker.com/_/postgres documentation.
Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.
I have created a docker-compose yml and .env file with the details you've provided and everything works fine as you can see from the pictures below:
I think your problem lies when you are passing the -h parameter.
Inside the container will always be localhost however, outside you will have to pass:
hostname: postgres
inside your docker-compose file so it will have the postgres hostname