Is there a way to pre-configure pgadmin (maybe via env variables) with some server connections?
Say you have this docker-compose.yml, something like PGADMIN_CONNECTIONS
env variable in this example? (PGADMIN_CONNECTIONS
is probably not a valid ENV variable, it's just an ilustration)
version: '3'
services:
postgres:
image: postgres
hostname: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: dbuser
POSTGRES_PASSWORD: dbpass
POSTGRES_DB: TEST_SM
pgadmin:
image: dpage/pgadmin4
ports:
- "80:80"
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: admin
??PGADMIN_CONNECTIONS: dbuser:dbpass@postgres:5432
Now let's connect pgadmin4 to our PostgreSQL database server. First, access the pgadmin4 via your favorite web browser by vising the URL http://localhost:5050/. Use the [email protected] for the email address and root as the password to log in. Create a server.
Launch pgAdmin 4. Go to the “Dashboard” tab. In the “Quick Link” section, click “Add New Server” to add a new connection. Select the “Connection” tab in the “Create-Server” window.
Setup PostgreSQL Using the Public Image We exposed the 5432 port on the host using the “-p 5432:5432” in the docker run command. To back up the data, we also mounted the /var/lib/postgresql/data directory to the /data directory of the host machine of the postgres container.
You need to define a servers.json file to configure connections. It's quite well documented here.
The default path inside the container is /pgadmin4/servers.json
. You could either COPY
your own version of the server.json file into a newly built image, or bind mount the file into the container when it is run.
The password cannot be passed to the pgadmin through JSON, but a file containing the password is referenced in the example below. See below for further information.
An example structure for the server.json
file is below:
{
"Servers": {
"1": {
"Name": "[email protected]",
"Group": "Servers",
"Host": "magic_db",
"Port": 5432,
"MaintenanceDB": "postgres",
"Username": "postgres",
"PassFile": "/pgpass",
"SSLMode": "prefer"
}
}
}
The /pgpass
requires the following structure:
hostname:port:database:username:password
So for the example above it would be:
magic_db:5432:postgres:postgres:secretpassword
Note: Password was changed to PassFile in newer versions of pgadmin as seen here.
This config works for me:
create an empty dir call postgres_db_data
create "pgpass" with the following contents
host.docker.internal:15432:postgres:postgres:postgres
{
"Servers": {
"1": {
"Name": "docker_postgres",
"Group": "docker_postgres_group",
"Host": "host.docker.internal",
"Port": 15432,
"MaintenanceDB": "postgres",
"Username": "postgres",
"PassFile": "/pgpass",
"SSLMode": "prefer"
}
}
}
version: "3.9"
services:
db:
image: postgres
volumes:
- ./postgres_db_data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "15432:5432"
pgadmin:
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: [email protected] #the username to login to pgadmin
PGADMIN_DEFAULT_PASSWORD: pgadmin # the password to login to pgadmin
ports:
- "5050:80"
volumes:
- ./servers.json:/pgadmin4/servers.json # preconfigured servers/connections
- ./pgpass:/pgpass # passwords for the connections in this file
depends_on:
- "db"
run docker compose up
open http://localhost:5050/browser/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With