≡ Menu

9 Steps to Install and Configure PostgreSQL from Source on Linux

PostgreSQL Installation and Configuration Steps
Similar to mySQL, postgreSQL is very famous and feature packed free and open source database.

Earlier we’ve discussed several installations including LAMP stack installation, Apache2 installation from source, PHP5 installation from source and mySQL installation.

In this article, let us review how to install postgreSQL database on Linux from source code.

Step 1: Download postgreSQL source code

From the postgreSQL download site, choose the mirror site that is located in your country.

# wget http://wwwmaster.postgresql.org/redir/198/f/source/v8.3.7/postgresql-8.3.7.tar.gz

Step 2: Install postgreSQL

# tar xvfz postgresql-8.3.7.tar.gz 

# cd postgresql-8.3.7

# ./configure
checking for sgmlspl... no
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking ./src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking ./src/backend/port/dynloader/linux.c to src/backend/port/dynloader.c
config.status: linking ./src/backend/port/sysv_sema.c to src/backend/port/pg_sema.c
config.status: linking ./src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking ./src/backend/port/dynloader/linux.h to src/include/dynloader.h
config.status: linking ./src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking ./src/makefiles/Makefile.linux to src/Makefile.port

# make
make[3]: Leaving directory `/usr/save/postgresql-8.3.7/contrib/spi'
rm -rf ./testtablespace
mkdir ./testtablespace
make[2]: Leaving directory `/usr/save/postgresql-8.3.7/src/test/regress'
make[1]: Leaving directory `/usr/save/postgresql-8.3.7/src'
make -C config all
make[1]: Entering directory `/usr/save/postgresql-8.3.7/config'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/usr/save/postgresql-8.3.7/config'
All of PostgreSQL successfully made. Ready to install. 

# make install
make -C test/regress install
make[2]: Entering directory `/usr/save/postgresql-8.3.7/src/test/regress'
/bin/sh ../../../config/install-sh -c  pg_regress '/usr/local/pgsql/lib/pgxs/src/test/regress/pg_regress'
make[2]: Leaving directory `/usr/save/postgresql-8.3.7/src/test/regress'
make[1]: Leaving directory `/usr/save/postgresql-8.3.7/src'
make -C config install
make[1]: Entering directory `/usr/save/postgresql-8.3.7/config'
mkdir -p -- /usr/local/pgsql/lib/pgxs/config
/bin/sh ../config/install-sh -c -m 755 ./install-sh '/usr/local/pgsql/lib/pgxs/config/install-sh'
/bin/sh ../config/install-sh -c -m 755 ./mkinstalldirs '/usr/local/pgsql/lib/pgxs/config/mkinstalldirs'
make[1]: Leaving directory `/usr/save/postgresql-8.3.7/config'
PostgreSQL installation complete.

PostgreSQL ./configure options

Following are various options that can be passed to the ./configure:

  • –prefix=PREFIX install architecture-independent files in PREFIX. Default installation location is /usr/local/pgsql
  • –enable-integer-datetimes  enable 64-bit integer date/time support
  • –enable-nls[=LANGUAGES]  enable Native Language Support
  • –disable-shared         do not build shared libraries
  • –disable-rpath           do not embed shared library search path in executables
  • –disable-spinlocks    do not use spinlocks
  • –enable-debug           build with debugging symbols (-g)
  • –enable-profiling       build with profiling enabled
  • –enable-dtrace           build with DTrace support
  • –enable-depend         turn on automatic dependency tracking
  • –enable-cassert         enable assertion checks (for debugging)
  • –enable-thread-safety  make client libraries thread-safe
  • –enable-thread-safety-force  force thread-safety despite thread test failure
  • –disable-largefile       omit support for large files
  • –with-docdir=DIR      install the documentation in DIR [PREFIX/doc]
  • –without-docdir         do not install the documentation
  • –with-includes=DIRS  look for additional header files in DIRS
  • –with-libraries=DIRS  look for additional libraries in DIRS
  • –with-libs=DIRS         alternative spelling of –with-libraries
  • –with-pgport=PORTNUM   change default port number [5432]
  • –with-tcl                     build Tcl modules (PL/Tcl)
  • –with-tclconfig=DIR   tclConfig.sh is in DIR
  • –with-perl                   build Perl modules (PL/Perl)
  • –with-python              build Python modules (PL/Python)
  • –with-gssapi               build with GSSAPI support
  • –with-krb5                  build with Kerberos 5 support
  • –with-krb-srvnam=NAME  default service principal name in Kerberos [postgres]
  • –with-pam                  build with PAM support
  • –with-ldap                  build with LDAP support
  • –with-bonjour            build with Bonjour support
  • –with-openssl            build with OpenSSL support
  • –without-readline      do not use GNU Readline nor BSD Libedit for editing
  • –with-libedit-preferred  prefer BSD Libedit over GNU Readline
  • –with-ossp-uuid        use OSSP UUID library when building contrib/uuid-ossp
  • –with-libxml               build with XML support
  • –with-libxslt               use XSLT support when building contrib/xml2
  • –with-system-tzdata=DIR  use system time zone data in DIR
  • –without-zlib              do not use Zlib
  • –with-gnu-ld              assume the C compiler uses GNU ld [default=no]

PostgreSQL Installation Issue1:

You may encounter the following error message while performing ./configure during postgreSQL installation.

# ./configure
checking for -lreadline... no
checking for -ledit... no
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.

PostgreSQL Installation Solution1:

Install the readline-devel and libtermcap-devel to solve the above issue.

# rpm -ivh libtermcap-devel-2.0.8-46.1.i386.rpm readline-devel-5.1-1.1.i386.rpm
warning: libtermcap-devel-2.0.8-46.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 1e5e0159
Preparing...                ########################################### [100%]
   1:libtermcap-devel       ########################################### [ 50%]
   2:readline-devel         ########################################### [100%]

Step 3: Verify the postgreSQL directory structure

After the installation, make sure bin, doc, include, lib, man and share directories are created under the default /usr/local/pgsql directory as shown below.

# ls -l /usr/local/pgsql/
total 24
drwxr-xr-x 2 root root 4096 Apr  8 23:25 bin
drwxr-xr-x 3 root root 4096 Apr  8 23:25 doc
drwxr-xr-x 6 root root 4096 Apr  8 23:25 include
drwxr-xr-x 3 root root 4096 Apr  8 23:25 lib
drwxr-xr-x 4 root root 4096 Apr  8 23:25 man
drwxr-xr-x 5 root root 4096 Apr  8 23:25 share

Step 4: Create postgreSQL user account

# adduser postgres

# passwd postgres
Changing password for user postgres.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

Step 5: Create postgreSQL data directory

Create the postgres data directory and make postgres user as the owner.

# mkdir /usr/local/pgsql/data

# chown postgres:postgres /usr/local/pgsql/data

# ls -ld /usr/local/pgsql/data
drwxr-xr-x 2 postgres postgres 4096 Apr  8 23:26 /usr/local/pgsql/data

Step 6: Initialize postgreSQL data directory

Before you can start creating any postgreSQL database, the empty data directory created in the above step should be initialized using the initdb command as shown below.

# su - postgres

# /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
The files belonging to this database system will be owned by user postgres
This user must also own the server process.

The database cluster will be initialized with locale en_US.UTF-8.
The default database encoding has accordingly been set to UTF8.
The default text search configuration will be set to "english".

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers/max_fsm_pages ... 32MB/204800
creating configuration files ... ok
creating template1 database in /usr/local/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

    /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
or
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

Step 7: Validate the postgreSQL data directory

Make sure all postgres DB configuration files (For example, postgresql.conf) are created under the data directory as shown below.

$ ls -l /usr/local/pgsql/data
total 64
drwx------ 5 postgres postgres  4096 Apr  8 23:29 base
drwx------ 2 postgres postgres  4096 Apr  8 23:29 global
drwx------ 2 postgres postgres  4096 Apr  8 23:29 pg_clog
-rw------- 1 postgres postgres  3429 Apr  8 23:29 pg_hba.conf
-rw------- 1 postgres postgres  1460 Apr  8 23:29 pg_ident.conf
drwx------ 4 postgres postgres  4096 Apr  8 23:29 pg_multixact
drwx------ 2 postgres postgres  4096 Apr  8 23:29 pg_subtrans
drwx------ 2 postgres postgres  4096 Apr  8 23:29 pg_tblspc
drwx------ 2 postgres postgres  4096 Apr  8 23:29 pg_twophase
-rw------- 1 postgres postgres     4 Apr  8 23:29 PG_VERSION
drwx------ 3 postgres postgres  4096 Apr  8 23:29 pg_xlog
-rw------- 1 postgres postgres 16592 Apr  8 23:29 postgresql.conf

Step 8: Start postgreSQL database

Use the postgres postmaster command to start the postgreSQL server in the background as shown below.

$ /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >logfile 2>&1 &
[1] 2222

$ cat logfile
LOG:  database system was shut down at 2009-04-08 23:29:50 PDT
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections

Step 9: Create postgreSQL DB and test the installation

Create a test database and connect to it to make sure the installation was successful as shown below. Once you start using the database, take backups frequently as mentioned in how to backup and restore PostgreSQL article.

$ /usr/local/pgsql/bin/createdb test

$ /usr/local/pgsql/bin/psql test
Welcome to psql 8.3.7, 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

test=#
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Robert Schultz June 17, 2009, 10:25 am

    Hello

    Thank you very much for the easy to follow build/install/run instructions. They’ve served me just great today 🙂

  • siva June 18, 2009, 3:39 am

    thanx s

  • siva June 18, 2009, 3:40 am

    hai…

    Thank you very much

  • raju June 18, 2009, 4:08 am

    hi
    iam using fedora8
    in default postgres is 8.3 iam install the source 8.3.7 in place of that
    i did all the commands according from INSTALL and README
    the commands are
    ./configure
    gmake
    su
    gmake install
    adduser postgres
    mkdir /usr/local/pgsql/data
    chown postgres /usr/local/pgsql/data
    su – postgres
    /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
    /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >logfile 2>&1 &
    /usr/local/pgsql/bin/createdb test
    /usr/local/pgsql/bin/psql test

    how to restart the postgresql service
    pls tel me any one

  • Ramesh Natarajan June 20, 2009, 11:55 pm

    @Robert Schultz, Siva,

    Thanks for your comment. I’m glad you found this guide helpful.

     

    @Raju,

    Refer to our PostgreSQL DBA Commands article that explain how to setup postgreSQL service and restart it.

  • Mike Davidson April 6, 2010, 8:59 am

    This easy rundown served me better than the official documentation on postgresql.org–at least for getting a testbed up and running quickly. Thank you.

  • mahesh August 4, 2010, 2:58 am

    thanks a lot,i am new to postgres and it helped me a lots,
    thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,

  • mahesh August 6, 2010, 1:41 am

    thanks a lot!!!!!

  • readline lib was readline6-dev October 1, 2010, 6:41 pm

    For Ubuntu 9.04 through 10.10 I had to sudo apt-get install readline6-dev.

  • ravindra May 9, 2011, 12:39 am

    hi how to enable uuid in postgres8.4.6

  • Nirmal July 28, 2011, 1:53 am

    hi
    I have installed postgres version “postgresql-8.3.7” on RHLE 5.2. but all the command not working properly like
    pg_restore
    service postgresql start|stop|status|restart
    so I want to uninstall the the postgresql, please tel me the steps.

  • beparas August 3, 2011, 7:06 am

    Hi,
    Thank you very much for this post.
    I download “postgresql-9.0.4” and cross-compile for my embedded board.
    Every things works fine till Step 8.
    When I try to start my postgreSQL database its gives me following error:
    “FATAL: could not write lock file “/tmp/.s.PGSQL.5432.lock”: No space left on device”

    The output of df -h is
    $df -h
    Filesystem Size Used Available Use% Mounted on
    /dev/root 3.6G 650.4M 2.8G 18% /
    tmpfs 424.0M 40.0K 424.0M 0% /dev
    shm 424.0M 0 424.0M 0% /dev/shm
    rwfs 512.0K 512.0K 0 100% /mnt/rwfs
    rwfs 512.0K 512.0K 0 100% /tmp
    rwfs 512.0K 512.0K 0 100% /var

    It shows that ‘/tmp’ is full, but ‘/’ is 2.8G free.
    I install all the FS on 4GB SD card.

    Please help me to solve this problem.
    Thanks in advance.

  • Anonymous August 27, 2011, 10:41 pm

    How to remove( completely ) postgresql, if I have installed and configured the PostgreSQL server according to your post above ?

  • Ravi jaikry October 20, 2011, 11:45 pm

    nice tutorial

  • Anonymous February 21, 2012, 6:59 am

    Thanks, very nice tutorial. For Linux Mint 12 (so probably Ubuntu 11.10 also) I also had to sudo apt-get install readline6-dev as mentioned above for older versions.

  • Anonymous April 4, 2012, 12:13 pm

    I have installed postgres 9.0.7 on my linux server and I can run the regression tests against the installed instance locally on my server. Next, I want to run this regression test against a remote machine which has my DB and app installed. Can someone tell me how I can run the regression test, where I have to make changes in configuration to run the test suite against remote host?

  • atoracle August 14, 2012, 8:36 pm

    Excellent post — clear, relavent, and concise. Extremely useful.

  • amit garg September 27, 2012, 12:18 am

    Excellent post

  • Anonymous March 14, 2013, 10:54 am

    I can’t execute certain commands because I’m not getting authenticated as a root user…What should i do then?

  • Navdeep March 25, 2013, 12:06 am

    Too Good Man

  • Babin Lonston July 26, 2013, 5:57 am

    Hi i have tried to install postgresql 9.2.4 from source tar.gz , after ./configure while im trying to make it as for gmake even after gmake installed its shows some error such as refer to INSTALL Document .. is there any other steps want to follow , is it differ from this posting steps ?

  • Navid August 8, 2013, 10:20 pm

    Thank you for posting this. Saved me a lot of time. Worked perfectly.

  • Babn Lonston August 16, 2013, 7:10 am

    configure: error: readline library not found
    If you have readline already installed, see config.log for details on the
    failure. It is possible the compiler isn’t looking in the proper directory.
    Use –without-readline to disable readline support.

    How to Fix ..

  • Navid August 21, 2013, 9:19 pm

    @Babn
    Try:
    sudo apt-get install libreadline-dev

  • Mehrad September 3, 2013, 7:19 pm

    You ROCK ! Thanks

  • kelvin May 9, 2014, 4:19 am

    hey i am trying to install postgres into my xubuntu and am etting the following error how do i correct it?

    The following packages have unmet dependencies:
    postgresql-9.3 : Depends: postgresql-client-9.3 but it is not going to be installed
    Depends: postgresql-common (>= 142~) but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.

  • Dhadabani March 27, 2015, 12:04 am

    Hi I getting error like ” No acceptable C Compiler found in $PATH ” how to solve this Please help me

  • Bob WasTaken May 1, 2015, 12:24 pm

    Also, the source comes with a startup script that can be installed by
    running:
    # cp contrib/start-scripts/linux /etc/init.d/postgresql
    # update-rc.d postgresql defaults
    (update-rc.d is to debian-based systems what chkconfig is to
    Redhat-based systems).

  • Dmitry September 13, 2015, 6:16 am

    Thank you so much! After constantly getting
    FATAL: role “postgres” does not exist
    this is the first instruction I found that works.

  • karthik January 20, 2016, 12:27 am

    Hi This is karthik

    Getting error while installing postgres 8.3.7 on linux 5.Please help

    -bash-3.2$ make install
    make -C doc install
    make[1]: Entering directory `/mnt/hgfs/postgres/postgresql-8.3.7/doc’
    mkdir -p — /usr/local/pgsql/doc/html
    mkdir: cannot create directory `/usr/local/pgsql’: Permission denied
    make[1]: *** [installdirs] Error 1
    make[1]: Leaving directory `/mnt/hgfs/postgres/postgresql-8.3.7/doc’
    make: *** [install] Error 2

  • Ronny March 6, 2017, 6:33 pm

    Just what I need, thank you for the clear and concise information.

  • Shynu March 19, 2019, 1:04 pm

    How about making the rpm from the source code?