Linux Tutorials, Guides & Latest Tech Stuffs

How to install PgAdmin4 on Ubuntu 18.04 Server mode

PgAdmin is an open source administration platform for PostgreSQL. It provides user interface to perform database operations from a browser. This post will describe step by step guide for how to install pgAdmin4 on Ubuntu 16.04 and Ubuntu 18.04 server mode. If you are looking for pgAdmin4 installation in desktop mode you can check my earlier blog.

If you are looking to install PostgreSQL, you can check following blog

>> Install PostgreSQL on Ubuntu 

 Update ubuntu environment

sudo apt-get update

Install Requirements

Build essentials and download other required pacakges

sudo apt-get install build-essential libssl-dev libffi-dev libgmp3-dev virtualenv python-pip libpq-dev python-dev

Create Virtual Environment

virtualenv .pgadmin4

Go to folder and Activate virtual environment

cd .pgadmin4
source bin/activate

Download Latest python wheel file

Get Latest pgAdmin4 python wheel file from pgAdmin4 official download page. On Download page click on Python Wheel and copy python wheel downloading link of latest release . Run a wget request to download file, in my case v4.3.0 is latest (released on April 13, 2018 )  in your case it may vary.

wget https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v3.0/pip/pgadmin4-3.0-py2.py3-none-any.whl

Install this python wheel file

I have installed pgAdmin4 for more than 50 times and I have found most common errors are permission denied and python module not found.  I have found if you install pgAdmin4 with no sudo you will get permission denied error for /var/lib/pgadmin4 and /var/log/pgadmin4/ even if you corrected this permission problem you will keep getting module not found error. So after trying lots of way to installing pgAdmin, I would suggest you to install pgAdmin python wheel file one with sudo and another with non sudo  . This will make installation super easy and error free

use pip to install recently downloaded PgAdmin4 python wheel file once without sudo and then with sudo.

pip install pgadmin4-3.0-py2.py3-none-any.whl
sudo pip install pgadmin4-3.0-py2.py3-none-any.whl

Configure PgAdmin4 using config_local.py

By default there is a file in PgAdmin4 that is called config.py, for configurations we can create another file called config_local.py to avoid messing with actual configurations.

vim ~/.pgadmin4/lib/python2.7/site-packages/pgadmin4/config_local.py

We will add  PgAdmin4 configuration

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'
SERVER_MODE = True

Run pgAdmin4 Python file with sudo

sudo python ~/.pgadmin4/lib/python2.7/site-packages/pgadmin4/pgAdmin4.py

After running python file you will asked to provide email and password that will be used to login in after setup completion

Now exit from virtualenv to setup apache2

deactivate

and correct permissions

sudo chown -R www-data:www-data /var/lib/pgadmin4/
sudo chown -R www-data:www-data /var/log/pgadmin4/

Configure and Setup Apache2 to run PgAdmin4 as WSGI application

As per the PgAdmin official recommendations PgAdmin4 Production installations should be run as a WSGI application behind apache2 or httpd. We will follow this recommendation and install apache2 first and then will enable wsgi module.

Install Apache2/ httpd and wsgi module

Install apache2

sudo apt-get install apache2 apache2-utils libexpat1 ssl-cert python

After successful execution of this command, you can use curl to verify Apache server is live

curl http://localhost

Install mod_wsgi

sudo apt-get install libapache2-mod-wsgi

Restart Apache

sudo /etc/init.d/apache2 restart

Configure apache2 to run PgAdmin4

go to  /etc/apache2/sites-available/ folder and create a configuration file for pgAdmin4

cd /etc/apache2/sites-available/
sudo touch pgadmin4.conf

Add configurations to this newly created file

sudo vim pgadmin4.conf

replace example.com with your domain/subdomain name. Correct file path as per your file locations.

<VirtualHost *>
    ServerName example.com

    WSGIDaemonProcess pgadmin processes=1 threads=25
    WSGIScriptAlias / /home/ubuntu/.pgadmin4/lib/python2.7/site-packages/pgadmin4/pgAdmin4.wsgi
 
    <Directory "/home/ubuntu/.pgadmin4/lib/python2.7/site-packages/pgadmin4/">
        WSGIProcessGroup pgadmin
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
    </Directory>
</VirtualHost>

Now go to /etc/apache2/sites-enabled/ folder

cd /etc/apache2/sites-enabled/

remove apache default configuration

sudo rm -r 000-default.conf

and create a symlink for pgadmin4 configurations in sites-available.

sudo ln -s ../sites-available/pgadmin4.conf .

Test your apache2 configurations

use apachectl configtest to test your apache configurations, on success you will see Syntax OK.

apachectl configtest

Restart apache2 

sudo service apache2 restart

Test your PgAdmin4 installation

As your PgAdmin4 setup is completed with apache2 server and wsgi module. You can monitor your apache access logs  ( /var/log/apache2/access.log ) or error logs (/var/log/apache2/error.log ) to know is every request for PgAdmin4 is actually reaching to your web server. Similarly you can monitor pgAdmin4 logs to know issues associated with PgAdmin4 installations. You can also use curl to know PgAdmin4 response.

curl localhost

you may get response like this on terminal

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><title>Redirecting...
</title> <h1>Redirecting...</h1> <p>You should be redirected automatically 
to target URL: <a href="/login?next=%2F">/login?next=%2F</a>. 
If not click the link.

PgAdmin 4 Installation Error

In case you are getting error like this

Traceback (most recent call last):
  File "/home/ubuntu/.pgadmin4/lib/python2.7/site-packages/pgadmin4/pgAdmin4.py", line 70, in <module>
    app = create_app()
  File "/home/ubuntu/.pgadmin4/lib/python2.7/site-packages/pgadmin4/pgadmin/__init__.py", line 310, in create_app
    schema_version = version.value
AttributeError: 'NoneType' object has no attribute 'value'

you need to remove old pgadmin installation files and rerun pgadmin 4 python

sudo rm -r /var/lib/pgadmin4/
 sudo python ~/.pgadmin4/lib/python2.7/site-packages/pgadmin4/pgAdmin4.py

In case, you are facing any issue with this tutorial, Please let us know by filling comment form given below or drop an email at nixgyd[at]gmail.com . We will reply back swiftly. Thanks for reading.

Note: I have updated this post on May 4, 2018 as per PgAdmin4 configuration recommendations.

Pranav K

Pranav K is a software engineer and all-round computer geek. His interests include AWS, Ubuntu and Wordpress

10 Comments

  1. the vim command does not work on vim ~/.pgadmin4/lib/python2.7/site-packages/pgadmin4/config_local.py running xubuntu 18

    • Hi Franklin,

      vim is just a text editor, you can use any text editor that works on server or you can install vim by using sudo apt install vim.

      Thanks for your comment

  2. I get Forbidden – You don’t have permission to access /
    on this server.

    help

    • I think, this is because of some typo/error in you pgadmin4.conf file.
      Check If you have provided correct path of pgadmin4 files in your configuration file and these folders are actually exist in that order too.

  3. you dont need to delete 000-default.conf, besides if you need to use subdomain, just change this

    for this

  4. pgAdmin4 user Reply to pgAdmin4

    Based on paths starting with home/ubuntu/.pgadmin4/…
    assumption is that you are running all the commands logged in as a user with username “ubuntu”. Another assumption is that that “ubuntu” user is a sudo user.

    1. Is it really need for to use sudo user for server mode (SERVER_MODE = True) pgAdmin4 installation?
    2. Why is owner of /var/log/pgadmin4/*
    “www-data ” user?

    Mine wish is that user running the server is an ordinary user (no sudo privileges) and he is the owner of all files and process that pgAdmin server is using.

    Will this work if I remove user’s sudo privileges and set him as owner of /var/log/pgadmin4/* instead of “www-data ” user?

  5. Hey ,
    nice tutorial. You wrote “replace example.com with your domain/subdomain name. Correct file path as per your file locations.”
    However I do not have a domain/domainname.

    I have an aws account and an ec2 server configured. Do I really need a own domain to run pgadmin? Or what can I do?
    Thanks for an answer.

  6. Hey Pranav,
    as I am running nginx instead of apache2 (because my Django-websites) , I want you to ask if I can use nginx. Do you have an idea what I have to change in my nginx settings to get it work?
    Thank you for your efforts.

    Best regards
    Jonathan

    • Hi Jonathan,

      Yes, you can run Django websites with Nginx, You can set up a reverse proxy as per live port of your application. I have personally not tried it but I think it should work that way.

      Pranav
      Nixgyd

Leave a Reply

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