How to Install PostgreSQL and pgAdmin in CentOS 8

PostgreSQL is a powerful, widely-used, open-source, multi-platform and advanced object-relational database system known for its proven architecture, reliability, data integrity, robust feature set, and extensibility.

pgAdmin is an advanced, open-source, full-featured, and web-based administration and management tool for the PostgreSQL database server.

In this article, you will learn how to install the PostgreSQL 12 database server and pgAdmin 4 in CentOS 8 Linux distribution.

Step 1: Installing PostgreSQL on CentOS 8

1. First, disable the built-in PostgreSQL module by running the following dnf command.

# dnf -qy module disable postgresql

2. Next, enable the official PostgreSQL Yum Repository as shown.

# dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

3. Next, install the PostgreSQL 12 server and client packages.

# dnf install postgresql12 postgresql12-server
Install PostgreSQL on CentOS 8
Install PostgreSQL on CentOS 8

4. Once the installation is complete, initialize the PostgreSQL database, then start the PostgreSQL-12 service and enable it to automatically start at system boot. Then check if the service is up and running, and is enabled as shown.

# /usr/pgsql-12/bin/postgresql-12-setup initdb 
# systemctl start postgresql-12
# systemctl enable postgresql-12
# systemctl status postgresql-12
# systemctl is-enabled postgresql-12
Initialize PostgreSQL Database
Initialize PostgreSQL Database

Step 2: Secure and Configure PostgreSQL Database

5. Next, secure the Postgres user account and the database administrative user account. Start by creating a password for a Postgres system user account using the passwd utility as shown.

# passwd postgres
Set PostgreSQL User Password
Set PostgreSQL User Password

6. Then switch to the Postgres system account and create a secure and strong password for PostgreSQL administrative database user/role as follows.

# su - postgres
$ psql -c "ALTER USER postgres WITH PASSWORD '[email protected]';"
$ exit
Set PostgreSQL Admin Password
Set PostgreSQL Admin Password

7. Now configure how the Postgres server will authenticate clients such as pgAdmin. The supported authentication methods include password-based authentication which uses one of these methods: md5, crypt, or password.

For this guide, we will configure md5 authentication method in the file /var/lib/pgsql/12/data/pg_hba.conf.

# vi /var/lib/pgsql/12/data/pg_hba.conf

Find the following lines and change the authentication method to md5 as highlighted in the screenshot.

host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
Configure PostgreSQL Client Authentication
Configure PostgreSQL Client Authentication

8. After saving the file, to apply the recent changes in the Postgres configuration, restart the Postgres service.

  
# systemctl restart postgresql-12

Step 3: Installing pgAdmin4 in CentOS 8

9. Now we will install pgAdmin 4 to manage the PostgreSQL database from the web. First, you need to enable the EPEL and PowerTools repositories which contain some of the dependencies.

# dnf install epel-release
# dnf --enablerepo=PowerTools install pgadmin4
Install PgAdmin in CentOS 8
Install PgAdmin in CentOS 8

10. Next, start the httpd service and enable it to auto-start at system boot, then check if it is up and running as shown.

# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd

Step 4: Configuring pgAdmin 4 in CentOS 8

11. Now create a configuration file for pgAdmin4 under the httpd configuration structure. Create a new configuration file from the provided sample as shown.

# cp /etc/httpd/conf.d/pgadmin4.conf.sample /etc/httpd/conf.d/pgadmin4.conf

12. Next, ensure that the httpd configuration syntax is ok, and restart the service.

# httpd -t
# systemctl restart httpd
Check HTTPD Configuration
Check HTTPD Configuration

13. Now you need to create a directory for pgAdmin logs and libraries by running the following commands.

# mkdir -p /var/lib/pgadmin4/
# mkdir -p /var/log/pgadmin4/

14. Then declare the location/path of the log file, SQLite database, session database and storage in the Python configuration file for pgAdmin in the file.

# vi /usr/lib/python3.6/site-packages/pgadmin4-web/config_distro.py

Copy and paste the following lines in the file.

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'
Configure PgAdmin
Configure PgAdmin

15. Now create a user account used to authenticate in the web interface and also create the configuration database.

# python3 /usr/lib/python3.6/site-packages/pgadmin4-web/setup.py
Create PgAdmin Web User
Create PgAdmin Web User

16. Next, set the required ownership of the pgAdmin directories created recently.

# chown -R apache:apache /var/lib/pgadmin4
# chown -R apache:apache /var/log/pgadmin4

17. If you have SELinux enabled, also set the correct SELinux context of the pgAdmin log directories. And allow HTTPD scripts and modules (pgAdmin in this case)to connect to the network.

 
# chcon -t httpd_sys_rw_content_t /var/log/pgadmin4 -R
# chcon -t httpd_sys_rw_content_t /var/lib/pgadmin4 -R
# setsebool -P httpd_can_network_connect 1

18. Now restart the httpd service to apply all the recent changes.

# systemctl restart httpd

18. If you have the firewalld service enabled and running, open port 80 and 443 in the firewall to allow traffic to the HTTPD web server as shown.

# firewall-cmd --permanent --zone public --add-port 80/tcp
# firewall-cmd --permanent --zone public --add-port 443/tcp
# firewall-cmd --reload

Step 5: Accessing pgAdmin Web Interface

19. To access the pgAdmin web interface, open a browser and navigate using the following URL.

http://SERVER_IP/pgadmin4
OR
http://localhost/pgadmin4

Once the login interface loads, use the email address and password you created in step 15 above to log in.

PgAdmin Web Console
PgAdmin Web Console

20. Next, add a new server connection clicking on “Add New Server”.

Add New Server in PgAdmin
Add New Server in PgAdmin

21. Then under the “General” tab, enter the following settings server Name and optionally leave a comment to describe the connection.

Enter Server Name
Enter Server Name

22. Then define the connection profile by filling in the following:

  • Host – host/IP address of the PostgreSQL server.
  • Port – defaults to 5432.
  • Maintenance Database – defaults should be Postgres.
  • Username – the database username. You can use Postgres.
  • Password – password for the above user.

Then click Save.

Define Connection Profile
Define Connection Profile

23. The new server should now appear under the list of servers as highlighted in the following screenshot.

New Server Details
New Server Details

24. When you click on the server name, its attributes should load under the Dashboard as shown in the following screenshot.

PgAdmin Server Dashboard
PgAdmin Server Dashboard

There you have it! You have successfully installed Postgresql 12 and pgAdmin 4 in CentOS 8. Reach us via the feedback form below for any thoughts and questions. You can find more information in the Postgresql 12 documentation and pgAdmin documentation.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

16 thoughts on “How to Install PostgreSQL and pgAdmin in CentOS 8”

  1. Hi Aron, thanks for this tutorial was very helpful, sometimes I got an error and I can’t use PgAdmin4 anymore, don’t sure what can be the cause (maybe the reboot) but now when I try to run:

    http://localhost/pgadmin4
    

    I receive an error:

    Internal Server Error
    The server encountered an internal error or misconfiguration and was unable to complete your request.
    Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.
    More information about this error may be available in the server error log.

    I found only two way to solve it but I’m not sure that are the bests:

    1. Repeat the setup.py “python3 /usr/lib/python3.6/site-packages/pgadmin4-web/setup.py” and the other points after that.
    2. Run “python3 /usr/lib/python3.6/site-packages/pgadmin4-web/pgAdmin4.py” and navigate to http://127.0.0.1:5050 (keeping the py running).
    3. How can I restore the original “process” and use PgAdmin4 by navigating to http://localhost/pgadmin4?

      Thank you in advance

    Reply
  2. Hello Aaron, Thanks very much for this tutorial. I am very much new to Linux and Postgres. I have followed your instructions on installing Postgres and pgadmin; I am getting the following error messages below.

    I would really appreciate it if you could take a look at it and tell me how to correct it.

    [[email protected] ~]$ sudo systemctl start httpd
    [sudo] password for david:
    Job for httpd.service failed because the control process exited with error code.
    See "systemctl status httpd.service" and "journalctl -xe" for details.
    [[email protected] ~]$ sudo systemctl status httpd.service
    ● httpd.service - The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
    Active: failed (Result: exit-code) since Mon 2020-04-20 09:11:36 EDT; 1min 22s ago
    Docs: man:httpd.service(8)
    Process: 14737 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
    Main PID: 14737 (code=exited, status=1/FAILURE)
    Status: "Reading configuration..."

    Apr 20 09:11:36 centos-nyc systemd[1]: Starting The Apache HTTP Server...
    Apr 20 09:11:36 centos-nyc httpd[14737]: [Mon Apr 20 09:11:36.045465 2020] [so:warn] [pid 14737:tid 139823543744768] A>
    Apr 20 09:11:36 centos-nyc httpd[14737]: AH00558: httpd: Could not reliably determine the server's fully qualified dom>
    Apr 20 09:11:36 centos-nyc httpd[14737]: (98)Address already in use: AH00072: make_sock: could not bind to address [::>
    Apr 20 09:11:36 centos-nyc httpd[14737]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0>
    Apr 20 09:11:36 centos-nyc httpd[14737]: no listening sockets available, shutting down
    Apr 20 09:11:36 centos-nyc httpd[14737]: AH00015: Unable to open logs
    Apr 20 09:11:36 centos-nyc systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
    Apr 20 09:11:36 centos-nyc systemd[1]: httpd.service: Failed with result 'exit-code'.
    Apr 20 09:11:36 centos-nyc systemd[1]: Failed to start The Apache HTTP Server.

    Thanks very much,
    Davoo

    Reply
    • @David

      Is there another web server or service running on port 80 on your serve? According to the status error, HTTPD can’t start because port 80 seems to be in use already.

      Run this command to check:

      # ss -tpln
      

      Share the output of the above command. Thanks.

      Reply
      • Hello Aaron, Thanks very much for responding. Appears two are running/listening on port 80 and send-Q 128.

        I would appreciate your suggestions on how to move forward.

        [[email protected] ~]$ ss -tpln
        State Recv-Q Send-Q Local Address:Port Peer Address:Port
        LISTEN 0 5 0.0.0.0:*
        LISTEN 0 128 0.0.0.0:5355 0.0.0.0:*
        LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
        LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
        LISTEN 0 32 0.0.0.0:*
        LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
        LISTEN 0 5 0.0.0.0:*
        LISTEN 0 128 0.0.0.0:*
        LISTEN 0 128 0.0.0.0:*
        LISTEN 0 5 0.0.0.0:*
        LISTEN 0 5 [::1]:4330 [::]:*
        LISTEN 0 128 [::]:5355 [::]:*
        LISTEN 0 128 [::]:111 [::]:*
        LISTEN 0 128 [::]:80 [::]:*
        LISTEN 0 128 [::]:22 [::]:*
        LISTEN 0 5 [::1]:631 [::]:*
        LISTEN 0 128 [::1]:5432 [::]:*
        LISTEN 0 128 [::1]:6010 [::]:*
        LISTEN 0 5 [::1]:44321 [::]:*
        LISTEN 0 128 *:9090 *:*

        Thanks, David

        Reply
      • Hello again Aaron, I figured it out! I had nginx active/running, so I had to stop and start it to get working…

        Thanks again,
        David

        Reply
          • @David

            Once again, you will get all you need to know about CentOS 8 on this site. Simply use the search feature to look for what you want to read about. If you do not understand anything in any guide, drop us a comment.

        • @David

          Ok, you can’t run two services on the same port. If you want to run NGINX as the primary web server, you need to configure HTTPD to run on a different port e.g port 8080. So when you accessing pgadmin, you add that port in the URL. Thanks.

          Reply
  3. Thanks. I was trying to install PostgreSQL and pgadmin4 on my CI-Server. I also visited a lot of videos and other how-tos, but nothing worked as well as your how-to does.

    It’s really perfect and when following EXACTLY step by step it, for me, guarantees a successful installation. I tried it multiple times on a CentOS8-VM.

    Reply
    • @Simon

      Good to know that this guide worked for you. We always try our best to create guides that are easy to replicate whether in a test environment or in production. Many thanks for the useful feedback.

      Reply
  4. Thanks. I was trying to install PostgreSQL on my Linux server, I visited a lot of videos and blogs but your post is perfect!

    Reply
  5. Hi Aaron, thanks for your tutorial it was very helpful, just one step that you need to change in step 15 if you have python3, just you need to use this line “python3 /usr/lib/python3.6/site-packages/pgadmin4-web/setup.py” to set up the pgadmin4.

    Reply

Got something to say? Join the discussion.

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