1. Overview
pgAdmin 4 is a cross-platform PostgreSQL administration tool for Linux. It provides a neat GUI to manage database objects, run SQL queries, and perform other database tasks. In addition, it also provides a web GUI if we prefer to run pgAdmin 4 in a browser.
In this tutorial, we’ll learn how to install pgAdmin 4 on Linux. We’ll specifically focus on the desktop version and cover various methods for installing pgAdmin 4 on Linux, such as using package managers, building from source, and running pgAdmin 4 via a Docker container.
2. Installing via Package Managers
The pgAdmin 4 team provides official DEB and RPM packages that we can install on Debian, Fedora, and their derivatives.
2.1. Debian, Ubuntu, and Derivatives
On Debian and Ubuntu, we can use apt to install pgAdmin 4 from its official repository. We simply add the repository to apt sources. However, we’ll need to add the public key first:
$ curl -fsS https://www.pgadmin.org/static/packages_pgadmin_org.pub | \
sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg
Next, we’ll add the repository configuration:
$ sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] \
https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) \
pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
Optionally, we can update the package cache if it wasn’t carried out automatically:
$ sudo apt update
On success, we’re ready to install pgAdmin4. However, we should know that the repository provides several packages. For instance, the pgadmin4 package will install the desktop and the web version:
$ sudo apt install -y pgadmin4
Similarly, we can also install only the desktop version:
$ sudo apt install -y pgadmin4-desktop
Alternatively, if we prefer the web version, we can install it as well:
$ sudo apt install -y pgadmin4-web
However, we’ll need to configure the web server for the web version by running a ready-made shell script:
$ sudo /usr/pgadmin4/bin/setup-web.sh
Finally, if needed, we can upgrade pgAdmin4:
$ sudo apt upgrade -y pgadmin4
2.2. Fedora, Red Hat, and Derivatives
On Fedora, CentOS Stream, and Red Hat, we can add the official repositories and install pgAdmin4 via DNF/YUM. For Fedora, we can simply add the Fedora repository:
$ sudo rpm -i https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-fedora-repo-2-1.noarch.rpm
Similarly, for RedHat, CentOS Stream, and Rocky Linux, we add the Red Hat repository:
$ sudo rpm -i https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat-repo-2-1.noarch.rpm
Once the repositories are added with no errors, we can go ahead and install pgAdmin 4:
# GUI and web versions
$ sudo dnf install -y pgadmin4
# Desktop version
$ sudo dnf install -y pgadmin4-desktop
# Web version
$ sudo dnf install -y pgadmin4-web
As usual, we’ll need to set up the web server if we aim to use the web version:
$ sudo /usr/pgadmin4/bin/setup-web.sh
Finally, if required, we can upgrade pgAdmin 4:
$ sudo dnf upgrade -y pgadmin4
2.3. Arch Linux and Derivatives
On Arch Linux, we can install pgAdmin 4 from the Arch User Repository (AUR). For that, we’ll need to clone the Arch package repository:
$ git clone https://aur.archlinux.org/pgadmin4-py.git
Then, we’ll navigate to the directory and make the package:
$ cd pgadmin4-py/ && makepkg -si --noconfirm
The command will pull the required dependencies and build pgAdmin 4.
3. Building pgAdmin 4
We can build and install pgAdmin 4 from the source if there is no official repository for our distribution. First of all, make sure the required build dependencies are installed:
- Python 3.6+
- python-venv
- python-pip
- libpq
3.1. Set Build Environment
Initially, we need to create the virtual environment:
$ python3 -m venv pgadmin4 && pgadmin4
Next, let’s activate the virtual environment:
$ source bin/activate
3.2. Package Installation
We install pgadmin4 from the official FTP server:
(pgadmin4) $ pip install https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v4.30/pip/pgadmin4-4.30-py3-none-any.whl
In addition to the pgadmin4 package, it will also install the required package dependencies.
3.3. Build Configuration
By default, pgadmin4 will build the web version. Therefore, we’ll need to provide explicit configuration for it. We can achieve this by modifying the configuration file:
(pgadmin4) $ vim lib/<python>/site-packages/pgadmin4/config_local.py
In the path, we need to replace
import os
DATA_DIR = os.path.realpath(os.path.expanduser(u'~/.pgadmin/'))
LOG_FILE = os.path.join(DATA_DIR, 'pgadmin4.log')
SQLITE_PATH = os.path.join(DATA_DIR, 'pgadmin4.db')
SESSION_DB_PATH = os.path.join(DATA_DIR, 'sessions')
STORAGE_DIR = os.path.join(DATA_DIR, 'storage')
SERVER_MODE = False
Notably, we disabled the SERVER_MODE by setting it to False.
3.4. Run pgAdmin 4
We’re now ready to run pgAdmin4:
(pgadmin4) $ python3 lib/<python>/site-packages/pgadmin4/pgAdmin4.py
As usual, we need to replace
3.5. Creating Shortcut
Of course, it’s a bit of work if we need to run pgAdmin 4 frequently. Therefore, we can create a handy shell script to automate this:
$ touch ~/.local/bin/pgadmin4 && chmod +x $_
Now, let’s populate the script with the commands:
#!/bin/bash env
cd ~/pgadmin4 && \
source bin/activate && \
python3 lib/<python>/site-packages/pgadmin4/pgAdmin4.py
Make sure
Lastly, we add ~/.local/bin to $PATH:
$ echo "export PATH="$HOME/.local/bin:$PATH" >> ~/.bashrc
4. Alternative: Running via Docker
We can also run pgAdmin 4 in a Docker container by using the official image. It’s available on Docker Hub, so we can easily pull it:
$ docker pull dpage/pgadmin4:latest
Then, we can run it inside the Docker container:
$ docker run -p 80:80 \
-e '[email protected]' \
-e 'PGADMIN_DEFAULT_PASSWORD=secretpassword' \
-d dpage/pgadmin4:latest
Furthermore, we can set additional environment variables if required.
5. Conclusion
In this article, we discussed how to install pgAdmin 4 on Linux. First, we installed pgAdmin 4 using a package manager from its official package repositories. Then, we learned how to build the web version of pgAdmin 4 from the source.
Finally, we covered how to run pgAdmin 4 inside a Docker container.