Tutorial

How To Install and Configure pgAdmin 4 in Server Mode on Ubuntu 18.04

How To Install and Configure pgAdmin 4 in Server Mode on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04

Introduction

pgAdmin is an open-source administration and development platform for PostgreSQL and its related database management systems. Written in Python and jQuery, it supports all the features found in PostgreSQL. You can use pgAdmin to do everything from writing basic SQL queries to monitoring your databases and configuring advanced database architectures.

In this tutorial, we’ll walk through the process of installing and configuring the latest version of pgAdmin onto an Ubuntu 18.04 server, accessing pgAdmin through a web browser, and connecting it to a PostgreSQL database on your server.

Prerequisites

To complete this tutorial, you will need:

Step 1 — Installing pgAdmin and its Dependencies

As of this writing, the most recent version of pgAdmin is pgAdmin 4, while the most recent version available through the official Ubuntu repositories is pgAdmin 3. pgAdmin 3 is no longer supported though, and the project maintainers recommend installing pgAdmin 4. In this step, we will go over the process of installing the latest version of pgAdmin 4 within a virtual environment (as recommended by the project’s development team) and installing its dependencies using apt.

To begin, update your server’s package index if you haven’t done so recently:

  1. sudo apt update

Next, install the following dependencies. These include libgmp3-dev, a multiprecision arithmetic library; libpq-dev, which includes header files and a static library that helps communication with a PostgreSQL backend:

  1. sudo apt install libgmp3-dev libpq-dev

Following this, create a few directories where pgAdmin will store its sessions data, storage data, and logs:

  1. sudo mkdir -p /var/lib/pgadmin4/sessions
  2. sudo mkdir /var/lib/pgadmin4/storage
  3. sudo mkdir /var/log/pgadmin4

Then, change ownership of these directories to your non-root user and group. This is necessary because they are currently owned by your root user, but we will install pgAdmin from a virtual environment owned by your non-root user, and the installation process involves creating some files within these directories. After the installation, however, we will change the ownership over to the www-data user and group so it can be served to the web:

  1. sudo chown -R sammy:sammy /var/lib/pgadmin4
  2. sudo chown -R sammy:sammy /var/log/pgadmin4

Next, open up your virtual environment. Navigate to the directory your programming environment is in and activate it. Following the naming conventions of the prerequisite Python 3 tutorial, we’ll go to the environments directory and activate the my_env environment:

  1. cd environments/
  2. source my_env/bin/activate

After activating the virtual environment, it would be prudent to ensure that you have the latest version of pip installed on your system. The version of pip that’s available from the default Ubuntu 18.04 repositories is version 9.0.1, while the latest version is 21.0.1. If you installed the python3-pip package as outlined in the prerequisite Python installation tutorial but you haven’t upgraded it to the latest version, you will run into problems when configuring pgAdmin in the next step.

To upgrade pip to the latest version, run the following command:

  1. python -m pip install -U pip

Following this, download the pgAdmin 4 source code onto your machine. To find the latest version of the source code, navigate to the pgAdmin 4 (Python Wheel) Download page. Click the link for the latest version ( v6.10, as of this writing), then on the next page click the link reading pip. From this File Browser page, copy the file link that ends with .whl — the standard built-package format used for Python distributions. Then go back to your terminal and run the following wget command, making sure to replace the link with the one you copied from the PostgreSQL site, which will download the .whl file to your server:

  1. wget https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v6.10/pip/pgadmin4-6.10-py3-none-any.whl

Next install the wheel package, the reference implementation of the wheel packaging standard. A Python library, this package serves as an extension for building wheels and includes a command line tool for working with .whl files:

  1. python -m pip install wheel

Then install pgAdmin 4 package with the following command:

  1. python -m pip install pgadmin4-6.10-py3-none-any.whl

Next, install Gunicorn, a Python WSGI server that will be used with Nginx to serve the pgadmin web interface later in the tutorial:

  1. python -m pip install gunicorn

That takes care of installing pgAdmin and its dependencies. Before connecting it to your database, though, there are a few changes you’ll need to make to the program’s configuration.

Step 2 — Configuring pgAdmin 4

Although pgAdmin has been installed on your server, there are still a few steps you must go through to ensure it has the permissions and configurations needed to allow it to correctly serve the web interface.

pgAdmin’s main configuration file, config.py, is read before any other configuration file. Its contents can be used as a reference point for further configuration settings that can be specified in pgAdmin’s other config files, but to avoid unforeseen errors, you should not edit the config.py file itself. We will add some configuration changes to a new file, named config_local.py, which will be read after the primary one.

Create this file now using your preferred text editor. Here, we will use nano:

  1. nano my_env/lib/python3.10/site-packages/pgadmin4/config_local.py

In your editor, add the following content:

environments/my_env/lib/python3.10/site-packages/pgadmin4/config_local.py
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

Here are what these five directives do:

  • LOG_FILE: this defines the file in which pgAdmin’s logs will be stored.
  • SQLITE_PATH: pgAdmin stores user-related data in an SQLite database, and this directive points the pgAdmin software to this configuration database. Because this file is located under the persistent directory /var/lib/pgadmin4/, your user data will not be lost after you upgrade.
  • SESSION_DB_PATH: specifies which directory will be used to store session data.
  • STORAGE_DIR: defines where pgAdmin will store other data, like backups and security certificates.
  • SERVER_MODE: setting this directive to True tells pgAdmin to run in Server mode, as opposed to Desktop mode.

Notice that each of these file paths point to the directories you created in Step 1.

After adding these lines, save and close the file. If you used nano, do so by press CTRL + X followed by Y and then ENTER.

With those configurations in place, run the pgAdmin setup script to set your login credentials:

  1. python my_env/lib/python3.10/site-packages/pgadmin4/setup.py

After running this command, you will see a prompt asking for your email address and a password. These will serve as your login credentials when you access pgAdmin later on, so be sure to remember or take note of what you enter here:

Output
. . . Enter the email address and password to use for the initial pgAdmin user account: Email address: sammy@example.com Password: Retype password:

With that, pgAdmin is fully configured. However, the program isn’t yet being served from your server, so it remains inaccessible. To resolve this, we will configure Gunicorn and Nginx to serve pgAdmin so you can access its user interface through a web browser.

Step 3 — Starting Gunicorn and Configuring Nginx

You will be using Gunicorn to serve pgAdmin as a web application. However, as an application server Gunicorn will only be available locally, and not accessible through the internet. To make it available remotely, you will need to use Nginx as a reverse proxy.

Having completed the prerequisite to set up Nginx as a reverse proxy, your Nginx configuration file will contain this:

/etc/nginx/sites-available/your_domain
server {
    listen 80;
    listen [::]:80;

    server_name your_domain www.your_domain;
        
    location / {
        proxy_pass http://unix:/tmp/pgadmin4.sock;
        include proxy_params;
    }
}

This reverse proxy configuration enables your Gunicorn server to be accessible in your local browser. Start your Gunicorn server with the pgAdmin application:

  1. gunicorn --bind unix:/tmp/pgadmin4.sock --workers=1 --threads=25 --chdir ~/environments/my_env/lib/python3.10/site-packages/pgadmin4 pgAdmin4:app
Output
[2022-08-29 00:19:11 +0000] [7134] [INFO] Starting gunicorn 20.1.0 [2022-08-29 00:19:11 +0000] [7134] [INFO] Listening at: unix:/tmp/pgadmin4.sock (7134) [2022-08-29 00:19:11 +0000] [7134] [INFO] Using worker: gthread [2022-08-29 00:19:11 +0000] [7135] [INFO] Booting worker with pid: 7135

Note: Invoking Gunicorn in this manner ties the process to your terminal. For a more long-term solution, invoke Gunicorn with a program like Supervisor. You can follow this tutorial on how to install and manage Supervisor on Ubuntu and Debian VPS.

With Gunicorn acting as an application server made accessible by your Nginx reverse proxy, you are ready to access pgAdmin in your web browser.

Step 4 — Accessing pgAdmin

On your local machine, open up your preferred web browser and navigate to your server’s IP address:

http://your_server_ip

Once there, you’ll be presented with a login screen similar to the following:

pgAdmin login screen

Enter the login credentials you defined in Step 2, and you’ll be taken to the pgAdmin Welcome Screen:

pgAdmin Welcome Page

Now that you’ve confirmed you can access the pgAdmin interface, all that’s left to do is to connect pgAdmin to your PostgreSQL database. Before doing so, though, you’ll need to make one minor change to your PostgreSQL superuser’s configuration.

Step 5 — Configuring your PostgreSQL User

If you followed the prerequisite PostgreSQL tutorial, you should already have PostgreSQL installed on your server with a new superuser role and database set up.

By default in PostgreSQL, you authenticate as database users using the “Identification Protocol,” or “ident,” authentication method. This involves PostgreSQL taking the client’s Ubuntu username and using it as the allowed database username. This can allow for greater security in many cases, but it can also cause issues in instances where you’d like an outside program, such as pgAdmin, to connect to one of your databases. To resolve this, we will set a password for this PostgreSQL role which will allow pgAdmin to connect to your database.

From your terminal, open the PostgreSQL prompt under your superuser role:

  1. sudo -u sammy psql

From the PostgreSQL prompt, update the user profile to have a strong password of your choosing:

  1. ALTER USER sammy PASSWORD 'password';

Then exit the PostgreSQL prompt:

  1. \q

Next, go back to the pgAdmin 4 interface in your browser, and locate the Browser menu on the left hand side. Right-click on Servers to open a context menu, hover your mouse over Create, and click Server….

Create Server context menu

This will cause a window to pop up in your browser in which you’ll enter info about your server, role, and database.

In the General tab, enter the name for this server. This can be anything you’d like, but you may find it helpful to make it something descriptive. In our example, the server is named Sammy-server-1.

Create Server - General tab

Next, click on the Connection tab. In the Host name/address field, enter localhost. The Port should be set to 5432 by default, which will work for this setup, as that’s the default port used by PostgreSQL.

In the Maintenance database field, enter the name of the database you’d like to connect to. Note that this database must already be created on your server. Then, enter the PostgreSQL username and password you configured previously in the Username and Password fields, respectively.

Create Server - Connection tab

The empty fields in the other tabs are optional, and it’s only necessary that you fill them in if you have a specific setup in mind in which they’re required. Click the Save button, and the database will appear under the Servers in the Browser menu.

You’ve successfully connected pgAdmin4 to your PostgreSQL database. You can do just about anything from the pgAdmin dashboard that you would from the PostgreSQL prompt. To illustrate this, we will create an example table and populate it with some sample data through the web interface.

Step 6 — Creating a Table in the pgAdmin Dashboard

From the pgAdmin dashboard, locate the Browser menu on the left-hand side of the window. Click on the plus sign (+) next to Servers (1) to expand the tree menu within it. Next, click the plus sign to the left of the server you added in the previous step (Sammy-server-1 in our example), then expand Databases, the name of the database you added (sammy, in our example), and then Schemas (1). You should see a tree menu like the following:

Expanded Browser tree menu

Right-click the Tables list item, then hover your cursor over Create and click Table….

Create Table context menu

This will open up a Create-Table window. Under the General tab of this window, enter a name for the table. This can be anything you’d like, but to keep things simple we’ll refer to it as table-01.

Create Table - General tab

Then navigate to the Columns tab and click the + sign in the upper right corner of the window to add some columns. When adding a column, you’re required to give it a Name and a Data type, and you may need to choose a Length if it’s required by the data type you’ve selected.

Additionally, the official PostgreSQL documentation states that adding a primary key to a table is usually best practice. A primary key is a constraint that indicates a specific column or set of columns that can be used as a special identifier for rows in the table. This isn’t a requirement, but if you’d like to set one or more of your columns as the primary key, toggle the switch at the far right from No to Yes.

Click the Save button to create the table.

Create Table - Columns Tab with Primary Key turned on

By this point, you’ve created a table and added a couple columns to it. However, the columns don’t yet contain any data. To add data to your new table, right-click the name of the table in the Browser menu, hover your cursor over Scripts and click on INSERT Script.

INSERT script context menu

This will open a new panel on the dashboard. At the top you’ll see a partially-completed INSERT statement, with the appropriate table and column names. Go ahead and replace the question marks (?) with some dummy data, being sure that the data you add aligns with the data types you selected for each column. Note that you can also add multiple rows of data by adding each row in a new set of parentheses, with each set of parentheses separated by a comma as shown in the following example.

If you’d like, feel free to replace the partially-completed INSERT script with this example INSERT statement:

INSERT INTO public."table-01"(
    col1, col2, col3)
    VALUES ('Juneau', 14, 337), ('Bismark', 90, 2334), ('Lansing', 51, 556);

Example INSERT statement

Click on the sideways triangle icon () to execute the INSERT statement. Note that in older versions of pgAdmin, the execute icon is instead a lightning bolt ().

To view the table and all the data within it, right-click the name of your table in the Browser menu once again, hover your cursor over View/Edit Data, and select All Rows.

View/Edit Data, All Rows context menu

This will open another new panel, below which, in the lower panel’s Data Output tab, you can view all the data held within that table.

View Data - example data output

With that, you’ve successfully created a table and populated it with some data through the pgAdmin web interface. Of course, this is just one method you can use to create a table through pgAdmin. For example, it’s possible to create and populate a table using SQL instead of the GUI-based method described in this step.

Conclusion

In this guide, you learned how to install pgAdmin 4 from a Python virtual environment, configure it, serve it to the web with Gunicorn and Nginx, and how to connect it to a PostgreSQL database. Additionally, this guide went over one method that can be used to create and populate a table, but pgAdmin can be used for much more than just creating and editing tables.

For more information on how to get the most out of all of pgAdmin’s features, we encourage you to review the project’s documentation. You can also learn more about PostgreSQL through our Community tutorials on the subject.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Mark Drake
Mark DrakeManager, Developer Education
See author profile

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
20 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Thanks for the tutorial! I have a problem, for some reason I cannot access the postgresql server :/ Not sure what is wrong… My server is running on a DO droplet. The port 5432 seems to be open: ~$ lsof -i :5432 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME postgres 23170 postgres 6u IPv4 9234028 0t0 TCP localhost:postgresql (LISTEN)

I have created a user/password and a database, but when I try to connect from my computer with HeidiSQL or pgAdmin, I get error messages like: “Is the server running on host “138.x.x.x” and accepting TCP/IP connections on port 5432?”

What am I missing?

Answering my own question after some googling and trial and error…

Allow incoming connection from any (or specific) IP addresses. Add the line listen_addresses = ‘*’ to the file /etc/postgresql/[VERSION]/main/postgresql.conf.

Enable the applicable user to access all or specific databases from any (or specific) IP addresses. Add the line host all [USERNAME] 0.0.0.0/0 md5 to the file /etc/postgresql/[VERSION]/main/pg_hba.conf.

KFSys
Site Moderator
Site Moderator badge
June 4, 2024

Heya all,

For anyone stumbling upon this, if you want to access your SQL whether it’s MySQL, PostgreSQL or something else entierly, you’ll need to allow your IP to reach that port.

Allowing the Port for the whole network is not recommendable as well so using IP addresses should be the way to go.

when deactivated the virtual environment the output was permission denied. Can i get the solution?

KFSys
Site Moderator
Site Moderator badge
June 5, 2024

When you encounter a “permission denied” error while trying to deactivate a virtual environment in Python, it can be surprising, as deactivating typically involves just unsetting environment variables and should not require any special permissions. Here are some possible explanations and solutions to address this issue:

1. Misunderstanding the Command

First, ensure that you are using the correct command to deactivate the virtual environment. It should simply be:

bashCopy code

deactivate

This command is a shell alias/function that gets activated when you source the virtual environment’s activation script. If you’re trying something else, like trying to execute a script or delete files, you might encounter permission issues.

2. Shell Configuration and Permissions

If the deactivate command itself is giving a “permission denied” error, it could be due to an issue with your shell’s configuration or permissions on your shell configuration files (e.g., .bashrc, .bash_profile). Check the permissions of these files:

ls -l ~/.bashrc
ls -l ~/.bash_profile

Ensure that you have read and execute permissions for these files. If not, you can set them with:

chmod +rx ~/.bashrc
chmod +rx ~/.bash_profile

3. Virtual Environment Script Permissions

It’s possible though unusual that the scripts within the virtual environment’s bin directory have incorrect permissions. Check the permissions of the activate and deactivate scripts:

ls -l /path/to/venv/bin/activate
ls -l /path/to/venv/bin/deactivate

If the permissions are incorrect, you can set them to be executable by the user:

chmod +x /path/to/venv/bin/activate
chmod +x /path/to/venv/bin/deactivate

Hey, currently the login page is: http://my_server_ip/login How can I change it, e. g. to http://my_server_ip/some_other_path/login ?

Thanks for an answer.

KFSys
Site Moderator
Site Moderator badge
June 3, 2024

You can do that by changing the folders of where pgAdmin is located.

Additionally, you can deny access to others for your pgAdmin with Nginx for a more secure way.

This was the first tutorial I followed from this site. Thank you very much for this post, it was 100% all the way through!

I’m getting a syntax error when trying to doing the vonfigtest. I check pgadmin4.conf several times and the path names are all correct. IP is correct. it says "syntax error on line on : invalid command ‘<virtualHost’, perhaps misspelled or defined by a module not included in the server configuration.“

I’m absolutely positive I inputted exactly as instructed

This is really strange. In my droplet console, all <'s are printed as >'s. could this be where the error is coming from? Can I upload screen shots?

solved by accessing the server through terminal and not through digital oceans’ console pop up window. so buggy wtf

Mark Drake
DigitalOcean Employee
DigitalOcean Employee badge
August 13, 2019

Hello @jacobcoro,

Thank you for bringing this to our attention. I was able to replicate this issue and brought it up to the team managing the Control Panel Console. They are actively working on correcting it.

I’m sorry for this inconvenience and any frustration it caused you.

I fallow step by step and in step 4 I got an error from apache:

“Forbidden You don’t have permission to access / on this server.”

How can I slove this?

Mark Drake
DigitalOcean Employee
DigitalOcean Employee badge
August 13, 2019

Hello @danielkrudolf,

Thank you for your comment. I was able to complete the tutorial as it’s written without any issues, although I was able to replicate that same error by not updating the content in the pgadmin4.conf file to match my own configuration. For example, instead of:

/etc/apache2/sites-available/pgadmin4.conf
. . .
    WSGIDaemonProcess pgadmin processes=1 threads=25 python-home=/home/sammy/environments/my_env
    WSGIScriptAlias / /home/sammy/environments/my_env/lib/python3.6/site-packages/pgadmin4/pgAdmin4.wsgi

    <Directory "/home/sammy/environments/my_env/lib/python3.6/site-packages/pgadmin4/">
. . .

My file reads:

/etc/apache2/sites-available/pgadmin4.conf
. . .
    WSGIDaemonProcess pgadmin processes=1 threads=25 python-home=/home/mark/environments/my_env
    WSGIScriptAlias / /home/mark/environments/my_env/lib/python3.6/site-packages/pgadmin4/pgAdmin4.wsgi

    <Directory "/home/mark/environments/my_env/lib/python3.6/site-packages/pgadmin4/">
. . .

I would recommend that you double-check that the names of the files and directories listed in your pgadmin4.conf file were entered correctly and that they align with the directories and files on your own system.

KFSys
Site Moderator
Site Moderator badge
June 4, 2024

Heya,

In such cases, I’ll recommend checking the DocumentRoot directive and seeing if that’s correct.

Also, see if you have configured the reverse proxy properly.

Is there a reason why we can’t just do:

sudo apt-get install pgadmin4 pgadmin4-apache2

Wouldn’t that essentially take care of things? What am I missing?

Mark Drake
DigitalOcean Employee
DigitalOcean Employee badge
August 13, 2019

Hello @dimitrov2k,

Thanks for your question. As far as I know, pgadmin4 isn’t available from the default Ubuntu repositories, so installing it with apt would require you to add an external repository. Additionally, the pgadmin documentation recommends running the program from a virtual environment. Because of this, I felt it was more straightforward to install it with pip.

Of course, it may be that installing with pip may not make sense for your purposes, in which case another installation method - like with apt - would be a sound alternative.

Thank for this great tutorial. I just meet the DO website and I’m loving it. I got an error message in Step3, after testing configuration file’s syntax:

cgobel@ZL-WORKSTATION:/$ apachectl configtest
AH00526: Syntax error on line 1 of /etc/apache2/sites-enabled/pgadmin4.conf:
Name duplicates previous WSGI daemon definition.
Action 'configtest' failed.
The Apache error log may have more information.

Could you help me to understand this error? Thanks

I figured it out that I had another .conf file, since I previously installed pgadmin4 from apt. Now the Syntax test is ok. But I’m not able to access the login pages.

Mark Drake
DigitalOcean Employee
DigitalOcean Employee badge
August 27, 2019

Hello @cfgobel, thank you for your comments.

I can’t say with any certainty what the cause of your errors are, but because you’ve installed pgadmin with both apt and pip it may be that there are other conflicting configuration files between the two installations. I would try making sure that both installations are fully removed from your server, then start fresh by installing with either apt or with pip, as outlined in this guide.

I had the same error number in the same step, but with this message:

Invalid command 'WSGIDaemonProcess', perhaps misspelled or defined by a module not included in the server configuration

Which ended up being the case, easily resolved with:

sudo a2enmod wsgi

Hi,

I did a mistake when I created the user, and made the installation under /root.

But when I finished, I got the error 4.3 Forbidden.

Then I moved the folder environments/ to the user created, /home/USER_NAME. But now the server isn’t answering, and after some time it gives Gateway Timeout.

Thanks, Carlos

KFSys
Site Moderator
Site Moderator badge
June 5, 2024

You can remove the user you create and recreate him again for easier usage.

To delete a user on Ubuntu, you can use the userdel command, which is designed specifically for this purpose. Here’s how to properly remove a user account along with their home directory and mail spool:

Deleting a User in Ubuntu

  1. Open Terminal: You can open a terminal window by pressing Ctrl+Alt+T or by searching for “terminal” in your system’s application launcher.

  2. Delete the User:

    • To delete the user without removing their home directory, you can use the following command:
sudo userdel username
-   Replace `username` with the actual username of the account you want to delete.
  1. Delete the User Along with Their Home Directory and Mail Spool:

    • If you also want to remove the user’s home directory along with their mail spool, you can use the -r option:
sudo userdel -r username
-   Again, replace `username` with the actual username of the account.
KFSys
Site Moderator
Site Moderator badge
June 5, 2024

You can also update the root folder of the user created.

To change a user’s home directory in Ubuntu, you can use the usermod command, which is designed for modifying a user account. Here’s how you can update a user’s home directory:

  1. Change the Home Directory: Use the usermod command with the -d option to specify the new home directory, and optionally use the -m option to move the contents of the user’s current home directory to the new location. Here’s the syntax:
sudo usermod -d /new/home/dir -m username
-   Replace `/new/home/dir` with the path to the new home directory.
-   Replace `username` with the actual username of the user for whom you want to change the home directory.

For example, if you want to change the home directory of user `john` to `/home/new_john`, you would use:
sudo usermod -d /home/new_john -m john

Running python my_env/lib/python3.6/site-packages/pgadmin4/setup.py results in error:

Traceback (most recent call last):
  File "my_env/lib/python3.6/site-packages/pgadmin4/setup.py", line 413, in <module>
    setup_db()
  File "my_env/lib/python3.6/site-packages/pgadmin4/setup.py", line 345, in setup_db
    create_app_data_directory(config)
  File "/home/scottj/environments/my_env/lib/python3.6/site-packages/pgadmin4/pgadmin/setup/data_directory.py", line 24, in create_app_data_directory
    _create_directory_if_not_exists(os.path.dirname(config.SQLITE_PATH))
  File "/home/scottj/environments/my_env/lib/python3.6/site-packages/pgadmin4/pgadmin/setup/data_directory.py", line 16, in _create_directory_if_not_exists
    os.mkdir(_path)
PermissionError: [Errno 13] Permission denied: '/var/lib/pgadmin'

I believe that’s correct as there is no /var/lib/pgadmin directory as per these instructions. Even if I do this;

sudo chmod 777 -R /var/log/pgadmin4/
sudo chmod 777 -R /var/lib/pgadmin4/

It still results in the same error so it doesn’t appear to be a permission issue? So where did this go wrong?

Mark Drake
DigitalOcean Employee
DigitalOcean Employee badge
January 22, 2020

Hello @Scottj, and thank you for your question.

I just went through the tutorial as it’s written and was able to run the python my_env/lib/python3.6/site-packages/pgadmin4/setup.py command successfully, so I’m not sure why you’re running into that error.

I tried replicating the error by deleting the /var/lib/pgadmin4 directory created in Step 1. However I received a slightly different error after running the setup script:

Output
Traceback (most recent call last): File "my_env/lib/python3.6/site-packages/pgadmin4/setup.py", line 413, in <module> setup_db() File "my_env/lib/python3.6/site-packages/pgadmin4/setup.py", line 345, in setup_db create_app_data_directory(config) File "/home/mark/environments/my_env/lib/python3.6/site-packages/pgadmin4/pgadmin/setup/data_directory.py", line 24, in create_app_data_directory _create_directory_if_not_exists(os.path.dirname(config.SQLITE_PATH)) File "/home/mark/environments/my_env/lib/python3.6/site-packages/pgadmin4/pgadmin/setup/data_directory.py", line 16, in _create_directory_if_not_exists os.mkdir(_path) PermissionError: [Errno 13] Permission denied: '/var/lib/pgadmin4'

The main difference in my output is that Python is complaining about permissions on the /var/lib/pgadmin4 directory, where in yours it’s complaining about the /var/lib/pgadmin directory. It may be that you installed an older version of pgAdmin, and instead of expecting these directories:

/var/lib/pgadmin4/
/var/log/pgadmin4/

It expects these:

/var/lib/pgadmin/
/var/log/pgadmin/

I’d recommend that you go back and ensure that you’ve downloaded the .whl file corresponding to the latest version of pgAdmin (v4.17 as of today). Alternatively, you could delete the /var/lib/pgadmin4 and /var/log/pgadmin4 directories and replace them with /var/lib/pgadmin and /var/log/pgadmin, since that’s what your setup.py file seems to expect.

Yes, not sure what’s wrong then. I did retrieve pgadmin4-4.17-py2.py3-none-any.whl.

I’ll start over, thanks.

KFSys
Site Moderator
Site Moderator badge
June 4, 2024

Make sure you have added the proper ownership for the directory. It should be www-data:www-data otherwise your setup will not be able to reach it.

Thanks for the great tutorial. I ran into this error below when called python my_env/lib/python3.7/site-packages/pgadmin4/setup.py

ErrorImportError: cannot import name ‘url_encode’ from ‘werkzeug’ (/home/akwari/environments/my_env/lib/python3.7/site-packages/werkzeug/init.py)

I have tried the two commands below, but did not pull through.

sudo chmod 777 -R /var/lib/pgadmin4/```

Downgrading to an earlier version of of the werkzeug package fixed the issue for me.

pip install werkzeug==0.16.0

Hello i’ve followed the tutorial & installed Pgadmin, but after configuring apache and adding the virtual host I cant access to my ip i’m getting a 403 message “Forbidden You don’t have permission to access this resource.”

Hi! First of all, thank you very much for the tutorial. I was able to go through it without major problems from beginning to end. However, navigating to my ip’s address in chrome (also firefox) shows a red screen with text: “404 not found. File not found error”.

I don’t know exactly why this may be happening since as I said I managed to get working all the steps, but I think (just intuition from what I have tried and googled) that could be something related to the firewall. Any help would be much appreciated!

Added info:

I have checked the ufw status and I have it disabled. I had also tried, the following tutorials: -https://computingforgeeks.com/how-to-install-pgadmin-4-on-ubuntu/ -https://www.journaldev.com/26285/install-postgresql-pgadmin4-ubuntu But all of them give me the same “404 not found. File not found error” red window.

Hi, thanks for the tutorial! How can I change the default port? I’ve more than one app running on my server and I’would like to change the listen address for pgadmin4, for example on port 8081. Thanks!

KFSys
Site Moderator
Site Moderator badge
June 4, 2024

To change the default port on which pgAdmin 4 is running, especially when it is served by Gunicorn behind Nginx, you need to adjust the Gunicorn configuration and the Nginx reverse proxy settings. Here’s how to proceed:

Step 1: Modify Gunicorn Configuration

If you are running pgAdmin manually using a Gunicorn command:

  1. Find your Gunicorn startup command, which might be part of a service file or a manual command you run. It usually looks something like this:
gunicorn --bind 0.0.0.0:5050 www-data:www-data
  1. Change the port number in the --bind option to your new desired port. For example, if you want to change it to port 5080, the command would be:
gunicorn --bind 0.0.0.0:5080 www-data:www-data

If Gunicorn is configured via a service file (systemd):

  1. Edit the service file for pgAdmin which could be located in /etc/systemd/system/ (the file name might vary, e.g., pgadmin.service):
sudo nano /etc/systemd/system/pgadmin.service

Modify the ExecStart line to change the port number. For instance:

ExecStart=/path/to/gunicorn --workers 4 --bind 0.0.0.0:5080 www-data:www-data

Reload systemd to apply the changes and restart the service:

sudo systemctl daemon-reload
sudo systemctl restart pgadmin.service

Step 2: Update Nginx Configuration

Now you need to update your Nginx configuration to reverse proxy requests to the new port.

  1. Edit your Nginx configuration file for pgAdmin. This file is likely in /etc/nginx/sites-available/. Open it with a text editor:
sudo nano /etc/nginx/sites-available/pgadmin.conf
  1. Modify the proxy_pass line to reflect the new port. For example:
location / {
    proxy_pass http://localhost:5080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

This comment has been deleted

    Hello, I have a problem when I try to set up the email and the password. (running this command : python my_env/lib/python3.6/site-packages/pgadmin4/setup.py I got this error : sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table version already exists. do you have any solution pls ?

    Mark Drake
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 25, 2021

    Hello @gharsalli31, and thank you for your comment.

    I went through this tutorial as it’s written and I was able to replicate your error. I found the problem was that the version of pip installed in the prerequisite Python tutorial was not up to date. This would lead to problems when installing pgAdmin, but those problems would only become apparent when trying to run the setup.py script as you describe in your comment.

    I’ve added instructions for upgrading pip to Step 1. With this command added to the procedure, I was able to complete the tutorial without issue.

    I hope that helps you to get things working correctly!

    HI @mdrake, I was trying to install the client pgadmin 4.30 and I still got the same error. So I moved to the client 5.1 (With pip upgraded) and it was enough to figure out my problem. Thanks a lot for your help :)

    Thanks for this awesome and elaborative tutorial. I want to host this in Nginx instead of the Apache server. Can you add the configuration part (step 3) for the Nginx server? Thanks in advance.

    KFSys
    Site Moderator
    Site Moderator badge
    June 4, 2024

    Here is a working Nginx configuration for pgadmin

    server {
        listen 80;
        server_name pgadmin.yourdomain.com;
    
        location / {
            proxy_pass http://localhost:5050;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    
        error_log /var/log/nginx/pgadmin_error.log;
        access_log /var/log/nginx/pgadmin_access.log;
    }
    

    Make sure to create a symlink to the sites-enabled directory and reload nginx afterwards.

    After a lot of failed attempts at the end with 500 error from apache. I just used this link: https://www.pgadmin.org/download/pgadmin-4-apt/ and installed pgadmin4

    This comment has been deleted

      Instructions related to www-data, mentioned in the beginning, were posted somewhere else?

      KFSys
      Site Moderator
      Site Moderator badge
      June 3, 2024

      You’ll need to change the ownership of a few folders to www-data

      sudo chown -R www-data:www-data/var/lib/pgadmin4
      sudo chown -R www-data:www-data/var/log/pgadmin4
      
      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

      New accounts only. By submitting your email you agree to our Privacy Policy

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.