I’m trying to add a postgres server to pgadmin4, through bash commands in a docker compose file.
Here’s my docker compose file:
version: '3.9'
services:
podcast_db:
image: postgres:15.3-alpine
container_name: podcast_db
ports:
- 5432:5432
volumes:
- podcast_db_data:/usr/share/podcast_db/data
env_file:
- ./database/.env.local.db
# instead, we could have created a `environment` section, with
# with the env vars in .env.local.db file.
podcast_pgadmin:
image: dpage/pgadmin4
container_name: pgadmin4_container
ports:
- 5050:80
env_file:
- ./database/.env.local.db
command: >
sh -c " sudo apt-get install jq
| TOKEN=$(curl -s -X POST -d "username=${PGADMIN_DEFAULT_EMAIL}&password=${PGADMIN_DEFAULT_PASSWORD}" ${PGADMIN_URL}/api/v1/auth/login | jq -r .access_token)
| curl -X POST -H "Authorization: Bearer ${TOKEN}" -H "Content-Type: application/json" -d '{
"name": "'${POSTGRES_NAME}'",
"host": "'${POSTGRES_HOST}'",
"port": '${POSTGRES_PORT}',
"ssl_mode": "prefer",
"maintenance_database": "'${POSTGRES_DB}'",
"username": "'${POSTGRES_USER}'",
"password": "'${POSTGRES_PASSWORD}'"
}' ${PGADMIN_URL}/api/v1/servers"
volumes:
podcast_db_data:
The part above, in the command section, was done with the help of chatGPT… yeah. 😀
I can add the server, using the pgadmin GUI, without any problem…
However, I want to add it from bash.
When I do docker compose up
, I don’t get any error messages in the terminal, and when I check through the GUI, no server has been added…
Here is the log of the pgadming container:
NOTE: Configuring authentication for SERVER mode.
pgAdmin 4 - Application Initialisation
======================================
postfix/postlog: starting the Postfix mail system
[2023-10-03 19:47:43 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2023-10-03 19:47:43 +0000] [1] [INFO] Listening at: http://[::]:80 (1)
[2023-10-03 19:47:43 +0000] [1] [INFO] Using worker: gthread
[2023-10-03 19:47:43 +0000] [91] [INFO] Booting worker with pid: 91
All help would be appreciated.
2
Answers
You could easily do that by mapping a servers.json(/pgadmin4/servers.json) file. Checkout https://www.pgadmin.org/docs/pgadmin4/7.7/container_deployment.html#examples
Then servers.json file will be automatically imported on start.
This is a detailed solution step by step. I have spent a little bit of time to solve it, I followed this post from the early days, but I have a problem with the port, I configed wrong port. Now I test sucessfully.
current folder:
with
name
ishostname
inpgdatabase
service