How to Install pgAdmin4 Web Application as a Service
pgAdmin
is the most popular and feature-rich Open Source administration and development platform for PostgreSQL
, the most advanced Open Source database in the world.
Add pgadmin4 Repository
To add the repository find out the release of the Linux distribution by using
lsb_release -cs
Create a file /etc/apt/sources.list.d/pgdg.list
and add the following line to add the repository
deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main
Import the repository signing key, and update the package lists
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
Now install pgadmin4
sudo apt-get install pgadmin4
Configure pgadmin4
To find out where is the files for pgadmin4 web portal are located run
whereis pgadmin4
This should give you the path to the pgadmin4
, usually, it is located at /usr/share/pgadmin4/
, in this path, there should be a directory called web
. So in /usr/share/pgadmin4/web
there is a file called config.py
. This file holds the default configurations. Create another file called config_local.py
and add the following lines
LOG_FILE = '/var/log/pgadmin4/pgadmin4.log'
SQLITE_PATH = '/var/lib/pgadmin4/pgadmin4.db'
SESSION_DB_PATH = '/var/lib/pgadmin4/sessions'
STORAGE_DIR = '/var/lib/pgadmin4/storage'
Make sure that the file permission to the above-mentioned directories is appropriate. The directories specified are writeable by the user that the web server processes will be running as, e.g. apache
or www-data
Now, create the appropriate directories by using the following commands
sudo mkdir -p /var/lib/pgadmin4/{sessions,storage}
sudo mkdir -p /var/log/pgadmin4/
To change the directory permissions, use the following commands
sudo chown -R www-data:www-data /var/lib/pgadmin4/
sudo chown -R www-data:www-data /var/log/pgadmin4/
To change the ownership of the configuration database to the user that the webserver processes will run as, for example, assuming that the webserver runs as www-data
user in group www-data
, and that the SQLite path is /var/lib/pgadmin4/pgadmin4.db
, use the following command
sudo chown www-data:www-data /var/lib/pgadmin4/pgadmin4.db
Run the following command to create the configuration database
sudo python3 setup.py
This will create the configuration database and ask for the admin username (email) and password to be set for the initial login.
pgadmin4 as a Service
To create a service form recently installed pgadmin4
, we need to install a couple of software tools to help us manage the service. The first software we are going to install is a wsgi
proxy server named gunicorn
and to manage and supervise services supervisor
will be used.
Install gUnicorn
Installing gunicorn is fairly simple. gunicorn
is a python based package so it can be installed with python’s package manager pip
. In this example, we are using python3
. To install gunicorn run the following – (use sudo -H
to make gunicorn available systemwide for all users)
sudo -H python3 -m pip install gunicorn
Now let’s take a note where gunicorn
has been installed so that we can use this later in the process. To do that, a simple command like below will do
whereis gunicorn
This should spit out the installation location for gunicorn
. The output should look something like this
gunicorn: /usr/local/bin/gunicorn
Install Supervisor
Supervisor can be installed through apt
package manager. To install supervisor
use the following command
sudo apt-get install supervisor
Once installed successfully, the configurations for supervisor
will be available generally in /etc/supervisor/supervisord.conf
and the program configurations that are managed by it will be available in /etc/supervisor/conf.d/
directory. We will come back here later in the process.
Creating gUnicorn Configuration
To use the available feature of a wsgi
server like gunicorn
we need a configuration file. The location of this configuration file can be set anywhere in the system but I prefer to keep it inside pgadmin4
directory so that it’s attached to pgadmin4. So create a file named gunicorn_config.py
in /usr/share/pgadmin4/web
and add the following content
command = '/usr/local/bin/gunicorn'
pythonpath = '/usr/share/pgadmin4/web/'
bind = '127.0.0.1:5050'
workers = 1
threads = 25
user = 'www-data'
group = 'www-data'
In command =
section, use the path of gunicorn
retrieved in the previous step with whereis
command
Save the file and move to the next step.
Creating Supervisor Program Configuration
Let’s head back to /etc/supervisor/conf.d
and create a file named pgadmin4.conf
with following content
[program:pgadmin4]
command = /usr/local/bin/gunicorn -c /usr/share/pgadmin4/web/gunicorn_config.py pgAdmin4:app
directory = /usr/share/pgadmin4/web/
user = www-data
Now, we are ready to start the service
Start pgadmin4 Service
To start pgadmin4
as a service with supervisor
use the following command
sudo supervisorctl start pgadmin4
To check the current status of pgadmin4
use status command
sudo supervisorctl status pgadmin4
This should give you an output similar as below
pgadmin4 RUNNING pid 1519, uptime 0:18:55
supervisorctl
also, responds to commands like stop
and restart
to stop and restart services. Now we are ready to use the pgadmin4 web application. So how can we log in? Where is the web application?
Browse
Open an web browser and go to – http://127.0.0.1:5050
Enjoy!