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.
Related
I am an error while creating a Pgadmin docker container.
I am trying to create a default server and add database connection on creation of docker container.
Please check below logs from pgadmin container.
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
waiting for server to start....2023-01-26 16:43:57.062 UTC [36] LOG: starting PostgreSQL 12.13 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219, 64-bit
2023-01-26 16:43:57.064 UTC [36] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-01-26 16:43:57.076 UTC [37] LOG: database system was shut down at 2023-01-26 16:43:56 UTC
2023-01-26 16:43:57.083 UTC [36] LOG: database system is ready to accept connections
done
server started
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
2023-01-26 16:43:57.171 UTC [36] LOG: received fast shutdown request
waiting for server to shut down....2023-01-26 16:43:57.173 UTC [36] LOG: aborting any active transactions
2023-01-26 16:43:57.176 UTC [36] LOG: background worker "logical replication launcher" (PID 43) exited with exit code 1
2023-01-26 16:43:57.179 UTC [38] LOG: shutting down
2023-01-26 16:43:57.198 UTC [36] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
Docker-compose file:
postgres:
# image: postgres:14.1-alpine
build:
context: ./
dockerfile: ./Docker/Postgres/Dockerfile
container_name: postgres
image: postgres
restart: always
ports:
- 5432:5432
networks:
- 'default'
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
expose:
- 5432
pgadmin4:
build:
context: .
dockerfile: ./Docker/PgAdmin/Dockerfile
container_name: pgadmin
image: pgadmin
depends_on:
- postgres
environment:
- PGADMIN_DEFAULT_EMAIL=test#test.com
- PGADMIN_DEFAULT_PASSWORD=test
volumes:
- ./Docker/PgAdmin/servers.json:/pgadmin4/servers.json
networks:
- 'default'
ports:
- 9050:80
restart: unless-stopped
healthcheck:
test: wget localhost/misc/ping -q -O - > /dev/null 2>&1 || exit 1
interval: 5s
timeout: 10s
retries: 5
I am using postgres:12-alpine3.16 and dpage/pgadmin4 images.
Can someone please help me to resolve the issue?
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.
I have been trying to connect my go application to postgresql db using docker-compose.
Here is my Dockerfile for the web server
FROM golang:latest
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN go mod download
EXPOSE 5000
CMD ["go","run","main.go"]
And this is my docker-compose.yml file
version: '3'
services:
db:
image: postgres:latest
restart: always
network_mode: bridge
environment:
POSTGRES_USER: factly
POSTGRES_PASSWORD: qwertyui
POSTGRES_DB: factly
ports:
- "5432:5432"
server:
build: .
depends_on:
- db
ports:
- "5000:5000"
But I can't connect to the postgresql db as it gives a connection refused error message from the
Go serverStarting factly_db_1 ...
Starting factly_db_1 ... done
Recreating factly_server_1 ...
Recreating factly_server_1 ... done
Attaching to factly_db_1, factly_server_1
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2020-08-19 06:11:17.151 UTC [1] LOG: starting PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-08-19 06:11:17.151 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2020-08-19 06:11:17.152 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2020-08-19 06:11:17.266 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-08-19 06:11:17.906 UTC [26] LOG: database system was shut down at 2020-08-19 05:56:39 UTC
db_1 | 2020-08-19 06:11:18.213 UTC [1] LOG: database system is ready to accept connections
server_1 | host=127.0.0.1 port=5432 user=factly password=qwertyui dbname=factly sslmode=disable
server_1 | 2020/08/19 06:11:21 dial tcp 127.0.0.1:5432: connect: connection refused
server_1 | exit status 1
factly_server_1 exited with code 1
I have tried running just the postgres container and connecting it with the Go Application which works perfectly fine but running the Go Application inside a container gives the same error(Which makes me feel the Go web server container couldn't connect outside of it's network).
How can I correct this?
I guess the problem is you are trying to connect to 127.0.0.1 from the go program instead use the hostname of the database container db.
Currently, I would like to mount logging folder for postgres, so that even after host machine restart, logging information is still preserved.
I use the following docker-compose.yml
docker-compose.yml
version: '2'
services:
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=55F4rGFwsXXXXXX
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- postgres_logs:/logs
# Make Postgres log to a file.
# More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs
volumes:
postgres_data:
postgres_logs:
However, I'm getting the following permission denied error.
postgres_1 | 2018-02-19 09:03:45.359 UTC [1] LOG: database system is shut down
postgres_1 | 2018-02-19 09:04:45.963 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2018-02-19 09:04:45.963 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2018-02-19 09:04:45.965 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2018-02-19 09:04:45.972 UTC [1] FATAL: could not open log file "/logs/postgresql-2018-02-19_090445.log": Permission denied
postgres_1 | 2018-02-19 09:04:45.974 UTC [1] LOG: database system is shut down
postgres_1 | 2018-02-19 09:05:46.741 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2018-02-19 09:05:46.741 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2018-02-19 09:05:46.744 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2018-02-19 09:05:46.753 UTC [1] FATAL: could not open log file "/logs/postgresql-2018-02-19_090546.log": Permission denied
postgres_1 | 2018-02-19 09:05:46.755 UTC [1] LOG: database system is shut down
postgres_1 | 2018-02-19 09:06:47.366 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2018-02-19 09:06:47.366 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2018-02-19 09:06:47.368 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2018-02-19 09:06:47.375 UTC [1] FATAL: could not open log file "/logs/postgresql-2018-02-19_090647.log": Permission denied
postgres_1 | 2018-02-19 09:06:47.377 UTC [1] LOG: database system is shut down
Anyone has idea how can I resolve such error?
2nd Edit
After some reading it seems that is not possible to do this the way you need. You would need to be able to define file ownership when declaring a volume with docker-compose, and this is something that is not supported by the docker engine. But there are a few workarounds that you can consider, check here for more details.
As a workaround you could create a Dockerfile extending postgres and add this:
# ...
RUN mkdir /logs
RUN chown postgres:postgres /logs
Edit
After some experimentation this is working:
version: '2'
services:
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=55F4rGFwsXXXXXX
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./:/logs:z
# Make Postgres log to a file.
# More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs
volumes:
postgres_data:
Original answer
I accidentally got it working by doing this, first running docker-compose up with this docker-compose file:
version: '2'
services:
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=55F4rGFwsXXXXXX
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- postgres_logs:/logs:z
# Make Postgres log to a file.
# More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
command: /bin/bash -c "mkdir -p /logs && chmod -R 777 /logs && postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs"
volumes:
postgres_data:
postgres_logs:
This fails with:
postgres_1 | "root" execution of the PostgreSQL server is not
permitted. postgres_1 | The server must be started under an
unprivileged user ID to prevent postgres_1 | possible system security
compromise. See the documentation for postgres_1 | more information
on how to properly start the server.
After reverting the changes to command and running again then works, but obviously this is not a solution so stick with the edited answer above.
the problem is the permission in host, not in container.
so in host,
chmod -R 777 postgres_logs
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