Pgadmin Docker container having an adding a default server on image creation process - postgresql

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?

Related

Docker: Adminer backend for PostgrSQL database times out

I'm trying to set up a simple database system using a PostgreSQL Docker container and an Adminer container as a backend. Note this is all running on a RaspberryPi 4.
docker-compose.yml:
services:
postgres:
image: postgres
restart: on-failure
ports:
- 5432:5432
env_file:
- ./.auth_file
volumes:
- db:/var/lib/postgresql/data
adminer:
image: adminer
restart: on-failure
ports:
- 8070:8080
env_file:
- ./.auth_file
depends_on:
- postgres
volumes:
db:
I then run it using docker-compose up:
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2021-04-01 13:25:25.919 UTC [1] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on arm-unknown-linux-gnueabihf, compiled by gcc (Debian 8.3.0-6) 8.3.0, 32-bit
postgres_1 | 2021-04-01 13:25:25.919 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-04-01 13:25:25.919 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-04-01 13:25:26.028 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-04-01 13:25:26.090 UTC [27] LOG: database system was shut down at 2021-04-01 13:20:41 UTC
postgres_1 | 2021-04-01 13:25:26.116 UTC [1] LOG: database system is ready to accept connections
adminer_1 | [Sun Jun 14 00:31:20 2071] PHP 7.4.16 Development Server (http://[::]:8080) started
adminer_1 | [Sun Jun 14 00:30:16 2071] [::ffff:192.168.16.1]:50886 Accepted
The last line shows that I tried to access localhost:8070 in the Browser right on the Pi. The page just loads forever and Adminer never returns the page.
Could this have to do with some firewall issues?
Thanks for any help!
Found the solution. This has to do with the Docker Container being unable to fetch the current time from the host, due to some version issues surrounding Docker, Alpine (the base image for Adminer) and libseccomp. This is also evident by the fact that Adminer shows entirely different timestamps than the Postgres-Container in my original post.
The full explanation can be found here: https://wiki.alpinelinux.org/wiki/Release_Notes_for_Alpine_3.13.0#time64_requirements
After following the steps linked above, the docker-compose.yml should look something like this:
adminer:
image: adminer
...
security_opt:
- seccomp=config/compose/default.json # required to avoid clock_gettime() errors
Replace config/compose/default.json with the relative path to the modified default.json.

Not able to connect to Postgres database from golang Docker container

I am trying to connect to a Postgres database in a separate container from another separate container that stores a Go server:
Pool, err = pgxpool.Connect(context.Background(),"postgres://postgres:postgres#postgres:5432/postgres")
After doing so, I receive the following error:
2020/09/25 13:40:08 failed to connect to `host=postgres user=postgres database=postgres`: hostname resolving error (lookup postgres on 192.168.65.1:53: no such host)
Here is the docker-compose.yml:
version: "3.8"
services:
postgres:
image: postgres:alpine
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
restart: always
server:
build: .
depends_on:
- postgres
ports:
- "2302:2302"
- "80:80"
restart: always
I am successfully able to connect to the Postgres database from my OS. Here are the Postgres container initialization logs:
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2020-09-25 15:37:50.529 UTC [1] LOG: starting PostgreSQL 13.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
postgres_1 | 2020-09-25 15:37:50.529 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-09-25 15:37:50.529 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-09-25 15:37:50.532 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-09-25 15:37:50.536 UTC [20] LOG: database system was shut down at 2020-09-25 15:37:49 UTC
postgres_1 | 2020-09-25 15:37:50.540 UTC [1] LOG: database system is ready to accept connections
Any clues would be helpful. Thank you in advance.
Try to share a network
version: "3.8"
services:
postgres:
image: postgres:alpine
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
restart: always
networks: [ "go_develop" ]
server:
build: .
depends_on:
- postgres
ports:
- "2302:2302"
- "80:80"
restart: always
networks: [ "go_develop" ]
networks:
go_develop:
driver: bridge

Can't connect Go Application to the postgres container in docker-compose

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.

How to use Netcat to check whether postgresql docker container is up

I want to see if docker container with PostgreSQL is ready using Netcat utility.
My entrypoint.sh script seems to be unable to spot the DB up and running.
When I login into docker and run NC in verbose mode I get
DNS fwd/rev mismatch: db != telegram_messages_db_1.telegram_messages_default
What am I doing wrong?
My setup
entrypoint.sh
echo "Waiting for postgres..."
while ! nc -z db 5432; do
sleep 0.1
done
echo "PostgreSQL started"
docker-compose.yaml
version: '3.7'
services:
messages:
build:
context: .
dockerfile: Dockerfile
entrypoint: ['/usr/src/app/entrypoint.sh'] # new
volumes:
- .:/usr/src/app
ports:
- 5001:5000
environment:
.....
depends_on:
- db
db:
build:
context: ./backend/db
dockerfile: Dockerfile
expose:
- 5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
docker-compose output
Attaching to telegram_messages_db_1, telegram_messages_messages_1
messages_1 | Waiting for postgres...
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2020-03-29 23:55:22.103 UTC [1] LOG: starting PostgreSQL 12.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.2.0) 9.2.0, 64-bit
db_1 | 2020-03-29 23:55:22.103 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2020-03-29 23:55:22.104 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2020-03-29 23:55:22.115 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-03-29 23:55:22.170 UTC [20] LOG: database system was shut down at 2020-03-29 23:46:56 UTC
db_1 | 2020-03-29 23:55:22.190 UTC [1] LOG: database system is ready to accept connections
Also...
If I run nc -z -v telegram_messages_db_1.telegram_messages_default 5432
I do get a nice response:
telegram_messages_db_1.telegram_messages_default [172.19.0.2] 5432 (postgresql) open
Removing all containers, rebooting and building from scratch solved the issue.
I will leave it here for posterity.

How to setup volume for postgres log folder - Permission denied error

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