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

  1. Download pgAdmin4
  2. Create a virtual environment, in this case
python3 -m venv /var/www/virtual_environments/pgadmin
  1. 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.
  2. Activate this environment
source /var/www/virtual_environments/pgadmin/bin/activate

 

  1. 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

 

  1. 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.

  1. 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.
  2. 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
  1. enable mod_wsgi. Note we use wsgi3 for Python 3
sudo apt-get install libapache2-mod-wsgi-py3 (note wsgi3 for Python3)
  1. 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

  1. Enable Apache site
a2ensite pgadmin

  1. Restart Apache
sudo service apache2 restart

 

  1. Now if you go to /pgadmin on your browser, you should be able to see the login page
  2. Now it’s time to revisit the location of pgAdmin
  3. 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

 

Deploy pgAdmin as a web service under venv environment
Tagged on:                             

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + = 13

This site uses Akismet to reduce spam. Learn how your comment data is processed.