CentOS 7 – Install PHP and Postgres

First time using CentOS. Thought it will be useful to list down the steps to install PHP and Postgres and get it to work together.

0. Always recommend updating

yum update
# update will update every currently installed package

1. Install PHP

yum install php
# Validate installation and version by executing php --info

2. Install PostgreSQL

yum install postgresql-server postgresql-contrib 

3. Install php-pgsql connector

yum install php-pgsql

4. Now start and enable postgres

service postgresql initdb
systemctl start postgresql
systemctl enable postgresql
service postgresql restart

5. Modify the pg_hba.conf file located under /var/lib/pgsql/data/. Find the lines that looks like this

host    all             all             127.0.0.1/32            ident
host    all             all             ::1/128                 ident

Then replace “ident” with “md5”, so they look like this:

host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

6. Create local user and set user password

adduser automation
sudo su
passwd automation

7. Login as postgres and connect to database. Create user role (with same password as shell) and new database

su postgres
pgsql
>CREATE DATABASE masterdb;
>CREATE ROLE automation WITH PASSWORD 'UserPassword';
>GRANT ALL PRIVILEGES ON DATABASE masterdb TO automation;
>ALTER ROLE automation WITH LOGIN; 

The last one was important because I wasn’t able to connect to the database. But not sure if this solved the problem because GRANT ALL PRIVILEGES should have taken care of it. Needs to be researched further.

8. Create a test connection script in PHP to validate database connection

//  Configure DB Parameters
$host = "localhost";
$dbname = "masterdb";
$dbuser = "automation";
$userpass = "UserPassword";

$con = pg_connect("host=$host
                                  dbname=$dbname
                                  user=$dbuser
                                  password=$userpass
                                  ");


if (!$con) {
        die('Could not connect');
}
else {
        echo ("Connected to local DB");
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s