5

I'd love to run pgadmin4 in our infrastructure in a way, that postgres servers would be preconfigured during docker build/1.st start.

I've tried to modify the internaly used /var/lib/pgadmin/pgadmin4.db sqlite DB on the 1.st start, which however results in an error in the UI (once selecting the particular postgres server:

definition of service "" not found

I've tried following:

Directory structure:

find ./  -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'       
|____
|____dump
| |____servergroup.csv
| |____server.csv
| |____import_db.sh
|____Dockerfile

Where Dockerfile is:

cat Dockerfile
# rebuild:
# docker build -t pgadmin4:3.0-custom .
# run:
# docker run --rm -it -e PGADMIN_DEFAULT_EMAIL=admin -e PGADMIN_DEFAULT_PASSWORD=admin -p8081:80 docker build -t pgadmin4:3.0-custom

FROM dpage/pgadmin4:3.0

COPY dump/ /dump

RUN \
    apk add --no-cache sqlite && \
    chmod +x /dump/import_db.sh && \
    # re rely on the current entrypoint.sh impl
    sed -i '/python run_pgadmin.py/a   \/dump\/import_db.sh' /entrypoint.sh && \
    cat /entrypoint.sh

In fact it just modifies the https://github.com/postgres/pgadmin4/blob/master/pkg/docker/entrypoint.sh to run import_db.sh script on the 1.st start.

Where dump/import_db.sh is:

cat dump/import_db.sh
#!/bin/sh

echo ".tables" | sqlite3 -csv /var/lib/pgadmin/pgadmin4.db

# remove header and `1,1,Servers` entry (would cause duplicates)
cat /dump/servergroup.csv | sed '1d' | grep -v 1,1,Servers > /tmp/servergroup.in.csv
echo "csv servergroup:"
cat /tmp/servergroup.in.csv

echo "DB servergroup:"
sqlite3 -csv -header /var/lib/pgadmin/pgadmin4.db "select * from servergroup;"
echo ".import /tmp/servergroup.in.csv servergroup" | sqlite3 -csv /var/lib/pgadmin/pgadmin4.db

# remove header
cat /dump/server.csv | sed '1d' > /dump/server.in.csv
echo "csv server:"
cat /dump/server.in.csv

echo "DB server:"
sqlite3 -csv -header /var/lib/pgadmin/pgadmin4.db "select * from server;"
echo ".import /dump/server.in.csv server" | sqlite3 -csv /var/lib/pgadmin/pgadmin4.db

Csv files contents:

cat dump/server.csv 
id,user_id,servergroup_id,name,host,port,maintenance_db,username,password,role,ssl_mode,comment,discovery_id,hostaddr,db_res,passfile,sslcert,sslkey,sslrootcert,sslcrl,sslcompression,bgcolor,fgcolor,service
1,1,2,servername,localhost,5432,postgres,postgres,"",,prefer,,,"","",,<STORAGE_DIR>/.postgresql/postgresql.crt,<STORAGE_DIR>/.postgresql/postgresql.key,,,0,,,

cat dump/servergroup.csv
id,user_id,name
2,1,my-group
1,1,Servers

Any idea how to fix my error? Or of any other approach that could provide me the pre-configured pgadmin4 docker container?

Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81

2 Answers2

10

The current version of image dpage/pgadmin is 4.24. This version has support for external configuration of server definition list (servers.json):

{
    "Servers": {
        "test": {
            "Name": "test",
            "Group": "Servers",
            "Port": 5432,
            "Username": "postgres",
            "Host": "postgres",
            "SSLMode": "prefer",
            "MaintenanceDB": "postgres"
        }
    }
}

Volume binding can be configured as below:

volumes:
    - ./servers.json:/pgadmin4/servers.json

First time container is started server groups and servers will be configured automaticaly.

UPD. JSON format has more fields which are optional. It's important that password can not be imported/exported in such way due to the obvious security reasons.

svaor
  • 2,205
  • 2
  • 19
  • 41
  • Looks like passwords can't be imported/exported via the json, which is incredibly disappointing and makes this feature incomplete. These docs gives more info on the servers.json file - https://www.pgadmin.org/docs/pgadmin4/development/import_export_servers.html – ArtOfWarfare Oct 28 '22 at 14:28
2

Looks this change the service column value to an empty string instead of NULL.

Can you try updating the value of service column to NULL

sqlite> UPDATE server SET service = NULL;

commit the changes and Restart pgAdmin4 & try again connecting to that server.

Murtuza Z
  • 5,639
  • 1
  • 28
  • 52