You have successfully installed PostgreSQL Server:
Adjust Iptables/Firewall
Next, adjust iptables to access postgresql from remote systems.On CentOS 6.x systems:
vi /etc/sysconfig/iptables
Add the following line:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
Save and exit the file. Restart iptables service.
service iptables restart
On CentOS 7 systems:
firewall-cmd --permanent --add-port=5432/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
Adjust SELinux
Run the following command to make PostgreSQL work if SELinux enabled on your system.
setsebool -P httpd_can_network_connect_db 1
You may not login to PostegreSQL if you didn’t run the above command.
Create New User and Database
For example, let us create a new user called "payel" with password "centos", and database called "sitedb".Switch to postgres user:
Create user payel.
Create database:
Now, login to the psql prompt, and set password and Grant access to the database sitedb for payel:
psql (9.5.0)
Type "help" for help.
postgres=# alter user payel with encrypted password 'centos';
ALTER ROLE
postgres=# grant all privileges on database sitedb to payel;
GRANT
postgres=#
Delete Users and Databases
To delete the database, switch to postgres user:
Enter command:
To delete a user, enter the following command:
Configure PostgreSQL-MD5 Authentication
MD5 authentication requires the client to supply an MD5-encrypted password for authentication. To do that, edit /var/lib/pgsql/9.5/data/pg_hba.conf file:
vi /var/lib/pgsql/9.5/data/pg_hba.conf
Add or Modify the lines as shown below
[...]
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 192.168.1.0/24 md5
# IPv6 local connections:
host all all ::1/128 md5
[...]
Restart postgresql service to apply the changes:
On CentOS 6.x systems:
service postgresql-9.5 restart
On CentOS 7 systems:
systemctl restart postgresql-9.5
Configure PostgreSQL-Configure TCP/IP
By default, TCP/IP connection is disabled, so that the users from another computers can’t access postgresql. To allow to connect users from another computers, Edit file/var/lib/pgsql/9.5/data/postgresql.conf:
vi /var/lib/pgsql/9.5/data/postgresql.conf
Find the lines:
[...]
#listen_addresses = 'localhost'
[...]
#port = 5432
[...]
Uncomment both lines, and set the IP address of your postgresql server or set ‘*’ to listen from all clients as shown below:
listen_addresses = '*'
port = 5432
Restart postgresql service to apply the changes:
On CentOS 6.x systems:
service postgresql-9.5 restart
On CentOS 7 systems:
systemctl restart postgresql-9.5