pgAdmin can be deployed as a web server therefore user doesn’t need to install any client to access DB. This also provides a securer way of accessing your database: suppose your web server is on the same network as your DB server, you can setup firewall rules to allow access to the web server on port 80 only, you don’t need to open port on DB server for public access.
The documentation (https://www.pgadmin.org/docs/pgadmin4/1.x/server_deployment.html) on how to deploy pgAdmin as web server is quite brief, it doesn’t show much detail about what to do after you download the whl installation file on Linux.
Below are end-to-end steps to help you deploy pgAdmin as a web service. The environment is Ubuntu 16, Apache2, Python 3.5, mod_wsgi, venv (virtual environment) and pgAdmin4
- Download pgAdmin4
- Create a virtual environment, in this case
python3 -m venv /var/www/virtual_environments/pgadmin
- Once the above is run, pgadmin is a virtual environment that holds a copy of all Python3 executables. From now on, this virtual environment is separate from your system default Python.
- Activate this environment
source /var/www/virtual_environments/pgadmin/bin/activate
- Now put pgAdmin.whl into this folder and install it
cd /var/www/virtual_environments/pgadmin pip install pgadmin4-2.0-py2.py3-none-any.whl
- After installation, pgAdmin is put under /var/www/virtual_environments/pgadmin/lib/python3.5/site-packages/pgadmin4
This is an inconvenient place but it reflects the fact that from Python’s perspective, pgAdmin is just a site specific module. As the whole purpose of creating the virtual environment was to hold pgAdmin, we’ll move it to a better location later.
- Configure pgAdmin, follow the steps described under Python section. Instead of putting files(such as pgadmin4.db, pgadmin4.log) under /var/lib, you can put all those under the virtual_environments/pgadmin folder.
- Ensure the following are writable by www-data (Apache’s run user), as well as their parent folder (this is a MUST otherwise you’ll probably get “sqlite3.OperationalError: unable to open database file” error)
pgadmin4.db pgadmin4.log sessions storage
- enable mod_wsgi. Note we use wsgi3 for Python 3
sudo apt-get install libapache2-mod-wsgi-py3 (note wsgi3 for Python3)
- Create a site configuration file under /etc/apache2/sites-available
<VirtualHost *:80> ServerName pgadmin WSGIDaemonProcess pgadmin python-home=/var/www/virtual_environments/pgadmin python-path=/var/www/virtual_environments/pgadmin/lib/python3.5/site-packages processes=1 threads=25 WSGIScriptAlias / /var/www/virtual_environments/pgadmin/lib/python3.5/site-packages/pgadmin4/pgAdmin4.wsgi <Directory /var/www/virtual_environments/pgadmin/lib/python3.5/site-packages/pgadmin4> WSGIProcessGroup pgadmin WSGIApplicationGroup %{GLOBAL} Require all granted </Directory> </VirtualHost>
Note the above the use of python-home and python-path
- Enable Apache site
a2ensite pgadmin
- Restart Apache
sudo service apache2 restart
- Now if you go to /pgadmin on your browser, you should be able to see the login page
- Now it’s time to revisit the location of pgAdmin
- Create folder /var/www/virtual_environments/pgadmin/web and move everything under //var/www/virtual_environments/pgadmin/lib/python3.5/site-packages/pgadmin4 to this folder
a. Modify sites-enabled/pgadmin.conf
<VirtualHost *:80> ServerName pgadmin WSGIDaemonProcess pgadmin python-home=/var/www/virtual_environments/pgadmin:/var/www/virtual_environments/pgadmin/lib/python3.5/site-packages processes=1 threads=25 WSGIScriptAlias / /var/www/virtual_environments/pgadmin/web/pgAdmin4.wsgi <Directory /var/www/virtual_environments/pgadmin/web> WSGIProcessGroup pgadmin WSGIApplicationGroup %{GLOBAL} Require all granted </Directory> </VirtualHost>
b. Now reload pgadmin.conf
sudo service apache2 reload