Install pgAdmin 4 as a service under linux

Now that pgAdmin 4 is out you may want to install it on your linux machine.

As for debian/ubuntu at least there aren't any packages build yet here's a way to do it.

Create a Python 3 virtual environment

You can do without a virtual environment but that's how I like to do things.

Install the needed packages (you can also build Python from source if you fancy another Python version that what you already have in your OS) with sudo apt-get install python3-dev python3-venv wget

Create a virtual environment and activate it with:
pyvenv-3.5 pgadmin4 && source pgadmin4/bin/activate

Download, configure and run

wget https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v1.5/pip/pgadmin4-1.5-py2.py3-none-any.whl
pip install wheel
pip install pgadmin4-1.5-py2.py3-none-any.whl

Edit the pgadmin4/lib/python3.5/site-packages/pgadmin4/config.py file and change the SERVER_MODE = True to SERVER_MODE = False.

Run python pgadmin4/lib/python3.5/site-packages/pgadmin4/setup.py and follow the setup wizard.

You can test that everything works as it should by doing python pgadmin4/lib/python3.5/site-packages/pgadmin4/pgAdmin4.py and by opening your web browser to http://localhost:5050

Create a service

Edit the pgadmin4/lib/python3.5/site-packages/pgadmin4/pgAdmin4.py file and add #!/usr/bin/env python as a first line.
Also make it executable by doing chmod u+x pgadmin4/lib/python3.5/site-packages/pgadmin4/pgAdmin4.py.

Now create a /etc/systemd/system/pgadmin4.service service file containing:

[Unit]
Description=Pgadmin4 Service
After=network.target

[Service]
User=qgis
Group=qgis
WorkingDirectory=/home/qgis/Downloads/pgadmin4/
# Point to the bin folder of your virtual environment
Environment="PATH=/home/qgis/Downloads/pgadmin4/bin"
ExecStart=/home/qgis/Downloads/pgadmin4/lib/python3.5/site-packages/pgadmin4/pgAdmin4.py
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Run the service with sudo systemctl start pgadmin4.

Test if everything is fine by doing sudo systemctl status.

Set it up to autostart when linux boots by doing a sudo systemctl enable pgadmin4.

That's it!

Tudor Bărăscu

Tudor Bărăscu

QGIS platform, PostgreSQL, Linux and other FOSS software passionate with experience in the water/wastewater sector.

Read More