DEV Community

Cover image for Install PostgreSQL & PhpPgAdmin on Centos 7
soufyane
soufyane

Posted on

Install PostgreSQL & PhpPgAdmin on Centos 7

In this article we are going to install Postgresql-12 with PhpPgAdmin on Centos 7:

After connect to your server follow these Steps:

Step 1: Install apache and Configure Firewall

run the following commands:

$ sudo yum install httpd

now run these commands to enable and start the apache service:

$ sudo systemctl start httpd.service
$ sudo systemctl enable httpd.service

now lets configure our firewall, first you need to start your firewalld service by running the following command:

$ systemctl start firewalld

then run the following commands and reload the firewalld service:

$ sudo firewall-cmd --zone=public --permanent --add-service=http
$ sudo firewall-cmd --zone=public --permanent --add-port=5432/tcp
$ sudo firewall-cmd --reload

Step 2: Install php

before moving to next steps, we need first to setup php in our machine, in my case i'am using php 7.4
just run the following commands and you'll have php installed

$ sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
$ sudo yum -y install yum-utils
$ sudo yum-config-manager --enable remi-php74
$ sudo yum update
sudo yum install php php-mbstring

now you can check from the terminal your php version by running:

$ php -v

check php version

Step 3: PostgreSQL installation

in this tutorial we are using PostgreSQL version 12, if you would like to install any other version you can visit this website and they will provide you with necessary commands to use.

Note !: on the following commands i'am using PostgreSQL 12 so dont forget to replace the number 12 with your own version.

so lets start the installation by running:
$ sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
then run:
$ sudo yum install -y postgresql12-server
the following commands are Optional to initialize the database and enable automatic start:
$ sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
$ sudo systemctl enable postgresql-12
$ sudo systemctl start postgresql-12

now we need to configure the Postgresql files:

run the follwing command (i'am using nano editor ,if you are using vim just replace nano with vi):
$ sudo nano /var/lib/pgsql/12/data/pg_hba.conf

find the section below:


# IPv4 local connections:
host    all             all             127.0.0.1/32          ident
# IPv6 local connections:
host    all             all             ::1/128               ident
Enter fullscreen mode Exit fullscreen mode

then modify the authentication method of local connections as below:

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
Enter fullscreen mode Exit fullscreen mode

now save and exit:
using nano: hit ctrl+o+enter then ctrl+x
using vim: hit :wq

now we will move to the next configuration file:
$ sudo nano /var/lib/pgsql/12/data/postgresql.conf
find the line:

#listen_addresses = 'localhost'
Enter fullscreen mode Exit fullscreen mode

replace it with:

listen_addresses = '*'
Enter fullscreen mode Exit fullscreen mode

then find:

#port = 5432
Enter fullscreen mode Exit fullscreen mode

replace it with:

port = 5432
Enter fullscreen mode Exit fullscreen mode

now save and exit as we did before.
finally for this step run these two commands:
$ sudo systemctl start postgresql-12.service
$ sudo systemctl enable postgresql-12.service

Bonus: Configure database informations:

this part is for creating a user and database from the terminal.
first run:
$ sudo -u postgres psql
then:
CREATE USER jhon ENCRYPTED PASSWORD '1234';

replace 'jhon' and '1234' with your own credentials!!

CREATE DATABASE dbname OWNER jhon;

additional info: if your database name contain "-" like for example "db-name" use the following command instead:
CREATE DATABASE "db-name" OWNER jhon;

finally run this command:
GRANT ALL PRIVILEGES ON DATABASE dbname TO jhon;

Step 4: Install and use PhpPgAdmin

to install phpPg admin run the following command:
$ sudo yum install phpPgAdmin
next pass direclty to the configuration by running:

$ sudo nano /etc/httpd/conf.d/phpPgAdmin.conf
Replace the line:

Require local
Enter fullscreen mode Exit fullscreen mode

with:

Require all granted
Enter fullscreen mode Exit fullscreen mode

and:

Deny from all
Enter fullscreen mode Exit fullscreen mode

with:

Allow from all
Enter fullscreen mode Exit fullscreen mode

now save and exit.
next move to the other config file:
$ sudo nano /etc/phpPgAdmin/config.inc.php
find the line:

$conf['servers'][0]['host'] = '';
Enter fullscreen mode Exit fullscreen mode

replace it with:

$conf['servers'][0]['host'] = 'localhost';
Enter fullscreen mode Exit fullscreen mode

then find:

$conf['owned_only'] = false;
Enter fullscreen mode Exit fullscreen mode

replace it with:

$conf['owned_only'] = true;
Enter fullscreen mode Exit fullscreen mode

save and exit.
that's it! now run these commands and you are ready to go!
$ sudo systemctl restart postgresql-12.service
$ sudo systemctl reload httpd.service
$ sudo systemctl restart httpd.service

Conclusion

by following all the steps above, you'll be able to run PostgreSQL on centos 7, and see your databases with PhpPgAdmin by visiting the following link: http://[your server ip]/phpPgAdmin/
good luck !

Top comments (0)