CentOS / RHEL Linux Install Postgresql Database Server

Q. How do I install and configure Postgresql database server under Red hat Enterprise Linux 5 / CentOS 5 / Fedora Linux?

A. The postgresql-server package includes the programs needed to create and run a PostgreSQL server, which will in turn allow you to create
and maintain PostgreSQL databases. PostgreSQL is an advanced Object-Relational database management system (DBMS) that supports almost all SQL constructs (including transactions, subselects and user-defined types and functions). You should install postgresql-server if you want to create and maintain your own PostgreSQL databases and/or your own PostgreSQL server. You also need to install the postgresql package.

You also need to install client package called postgrsql. This package contains the docs in HTML for the whole package, as well as command-line utilities for managing PostgreSQL databases on a PostgreSQL server.

Install Postgrsql Server

Login as the root user and enter the command:
# yum install postgresql postgresql-server

Loading "installonlyn" plugin
Setting up Install Process
Setting up repositories
Reading repository metadata in from local files
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for postgresql-server to pack into transaction set.
postgresql-server-8.1.9-1 100% |=========================|  87 kB    00:00     
---> Package postgresql-server.i386 0:8.1.9-1.el5 set to be updated
--> Running transaction check
--> Processing Dependency: postgresql = 8.1.9-1.el5 for package: postgresql-server
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for postgresql to pack into transaction set.
postgresql-8.1.9-1.el5.i3 100% |=========================| 119 kB    00:00     
---> Package postgresql.i386 0:8.1.9-1.el5 set to be updated
--> Running transaction check

Dependencies Resolved

 Package                 Arch       Version          Repository        Size 
 postgresql-server       i386       8.1.9-1.el5      updates           4.0 M
Installing for dependencies:
 postgresql              i386       8.1.9-1.el5      updates           2.8 M

Transaction Summary
Install      2 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)         

Total download size: 6.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): postgresql-server- 100% |=========================| 4.0 MB    00:05     
(2/2): postgresql-8.1.9-1 100% |=========================| 2.8 MB    00:04     
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: postgresql                   ######################### [1/2] 
  Installing: postgresql-server            ######################### [2/2] 

Installed: postgresql-server.i386 0:8.1.9-1.el5
Dependency Installed: postgresql.i386 0:8.1.9-1.el5

Start Postgresql

Type the following two command:
# chkconfig postgresql on
# service postgresql start


Initializing database:                                     [  OK  ]
Starting postgresql service:                               [  OK  ]

Connect to Postgresql Server

Connect to server using
# su - postgres
Connect using psql command line tool:
$ psql -d template1 -U postgres

Welcome to psql 8.1.9, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

Open TCP port 5432

Finally make sure iptables allows remote access to Postgresql database server:
Open /etc/sysconfig/iptables file:
# vi /etc/sysconfig/iptables
Append following line before COMMIT line to open port 5432:

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

Save and close the file. Restart the firewall:
# service iptables restart

See also: PostgreSQL add or create a user account and grant permission for database

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 19 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
19 comments… add one
  • shyam Apr 24, 2008 @ 6:33

    very accurate. thanks.

  • dlugasx Jan 29, 2009 @ 9:34

    good job!

    Please add some information how to setup database, user and password.

    Would be great to have some info about connection to database too .

    SSH tunel or normal way ;)

    cyberciti save admin ass ;)

  • Hashim Ibrahim May 4, 2009 @ 19:47

    Thank you, very good explanation .
    Please add some information how to setup database, user and password.
    Please add where is the location of the user name and password config file.where i can see the user names and password stored.

  • Patrick Jun 10, 2009 @ 13:37

    I was wondering about using a non-standard location for the database. For instance isn’t the data stored in /var/? I have a raid array setup and would like to store it there. I haven’t see many docs that refer to using another location.

  • Lou Jul 3, 2009 @ 10:57

    Hi ,
    I have a little trouble :

    in this step : Open TCP port 5432
    Open /etc/sysconfig/iptables file:
    # vi /etc/sysconfig/iptables

    I have : iptables-config not iptables

    And inside it there’s no COMMIT line !!! :(

    Wha’ts wrong ?? It’s not de correct file ?? Or just it’s a missing line ??

  • Luis Jul 15, 2009 @ 12:15


    I’ve done the same by creating a symlink “pgsql” at /var/lib, pointing it to my RAID device.



  • Erick Jul 18, 2009 @ 14:32

    When will CentOS offer postgresql 8.4 natively?

  • comanche Nov 10, 2009 @ 21:40

    2 lou
    iptables-save > /etc/sysconfig/iptables

  • Devendra Jun 7, 2010 @ 6:52

    I have done the same thing but I get an error as

    Starting postgresql service: [FAILED]

    can you please help me out to solve this problem..

  • kap$hure Aug 4, 2010 @ 22:34


    did you try initializing the dB?

    service postgresql initdb

  • amit Aug 5, 2010 @ 8:01

    Thanx a ton…exactly what i needed

  • dlaz Oct 6, 2010 @ 3:19

    [root@localhost ~]# service iptables restart
    Flushing firewall rules: [ OK ]
    Setting chains to policy ACCEPT: filter [ OK ]
    Unloading iptables modules: [ OK ]
    Applying iptables firewall rules: iptables-restore: line 27 failed

    why it failed? pls help….

  • paulcano Nov 10, 2011 @ 1:17

    On CetOS 6 you have to run ‘service postgresql initdb’ first before starting postgresql.

  • zven84 Apr 6, 2012 @ 15:43

    Thks dude

  • Bashir May 23, 2012 @ 19:53

    Very helpful. Thanks

  • Monty Joe Jul 31, 2012 @ 14:16

    Great thanks to you, sir.

  • Matt Nov 22, 2012 @ 21:51

    i have problem:
    [root@vs ~]# service postgresql restart
    Stopping postgresql service: [ OK ]
    Starting postgresql service: /etc/init.d/postgresql: line 114: echo: write error: Operation not permitted
    [ OK ]
    what can i do

  • Hrishi Mar 5, 2013 @ 0:04

    I tried “service postgresql initdb”…… the result was [ OK ]
    but when i used “service postgresql start”… the result was [FAILED]
    Can you tell me how to care take of this error?

  • Geoffrey Clark Mar 19, 2013 @ 7:55

    Before starting the service, I got this error:
    /var/lib/pgsql/data is missing. Use “service postgresql initdb” to initialize the cluster first.
    fortunately, it gave a very helpful recommendation, so after “service postgresql initdb”, I continued on with success, thanks!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum