PostgreSQL 10 Source Code Installation On RedHat 7

PostgreSQL can be installed using yum, RPM packages or by compiling source code. I have already covered basic installation using yum. In this post I will going to cover Source Code based installation. So let’s begin.

Install prerequisite packages.

[root@demunix ]# yum install gcc zlib-devel readline-devel

Make sure you have directory structure (Either on OS Mountpoint or Dedicated Mountpoint) for binaries and data files. I have dedicated mountpoints for Binaries (/pg_bin), for Database Cluster (/pg_data), for WAL Archives(/archive) and for Backup (/backup) as listed below.

[root@demunix ~]# df -hTP
Filesystem                  Type      Size  Used Avail Use% Mounted on
devtmpfs                    devtmpfs  963M     0  963M   0% /dev
tmpfs                       tmpfs     987M     0  987M   0% /dev/shm
tmpfs                       tmpfs     987M  9.7M  978M   1% /run
tmpfs                       tmpfs     987M     0  987M   0% /sys/fs/cgroup
/dev/mapper/ol_demunix-root xfs        50G  9.3G   41G  19% /
/dev/xvda1                  xfs      1014M  295M  720M  30% /boot
/dev/mapper/ol_demunix-home xfs       147G   41M  147G   1% /home
tmpfs                       tmpfs     198M   16K  198M   1% /run/user/42
tmpfs                       tmpfs     198M     0  198M   0% /run/user/0
/dev/xvdb1                  ext3      4.8G   11M  4.6G   1% /pg_bin
/dev/xvdc1                  ext3       15G   38M   14G   1% /pg_data
/dev/xvde1                  ext3      9.8G   23M  9.2G   1% /backup
/dev/xvdf1                  ext3      9.8G   23M  9.2G   1% /archive

Check these posts to know more about attaching a disk and mounting it on a desired mountpoint.

And directory structure,

[root@demunix postgresql-10.11]# mkdir -p /pg_bin/pgsql/10.11/
[root@demunix postgresql-10.11]# mkdir -p /pg_data/pgsql/10.11/data
[root@demunix postgresql-10.11]# mkdir -p /backup/pgsql/full_backup/
[root@demunix postgresql-10.11]# mkdir -p /archive/pgsql/10.11/wal_archives

Let’s now download source code from postgresql ftp site.
https://www.postgresql.org/ftp/source/

Directly download to your server if it has internet connectivity. Or download it in your PC and upload to server using ftp.

[root@demunix ~] cd /tmp
[root@demunix tmp]# wget https://ftp.postgresql.org/pub/source/v10.11/postgresql-10.11.tar.gz
--2020-03-26 15:30:20--  https://ftp.postgresql.org/pub/source/v10.11/postgresql-10.11.tar.gz
Resolving ftp.postgresql.org (ftp.postgresql.org)... 147.75.85.69, 217.196.149.55, 72.32.157.246, ...
Connecting to ftp.postgresql.org (ftp.postgresql.org)|147.75.85.69|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 24887345 (24M) [application/x-tgz]
Saving to: ‘postgresql-10.11.tar.gz’

100%[==================================================================================================================================================================>] 2,48,87,345 47.5KB/s   in 8m 39s 

2020-03-26 15:39:00 (46.8 KB/s) - ‘postgresql-10.11.tar.gz’ saved [24887345/24887345]

Extract it.

[root@demunix tmp]# tar -xvf postgresql-10.11.tar.gz

Navigate to extracted directory.

[root@demunix tmp]# cd postgresql-10.11/

Run configure to specify directory where you want to install your binaries.

[root@demunix postgresql-10.11]# ./configure --prefix=/pg_bin/pgsql/10.11/

If you find no error in configure, you’re good to proceed with make.

Compile the source code using make command.

[root@demunix postgresql-10.11]# make
make -C src all
make[1]: Entering directory `/tmp/postgresql-10.11/src'
make -C common all
make[2]: Entering directory `/tmp/postgresql-10.11/src/common'
make -C ../backend submake-errcodes
make[3]: Entering directory `/tmp/postgresql-10.11/src/backend'
prereqdir=`cd 'utils/' >/dev/null && pwd` && \
  cd '../../src/include/utils/' && rm -f errcodes.h && \
  ln -s "$prereqdir/errcodes.h" .
make[3]: Leaving directory `/tmp/postgresql-10.11/src/backend'
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -DFRONTEND -I../../src/include  -D_GNU_SOURCE  -DVAL_CONFIGURE="\"'--prefix=/pg_bin/pgsql/10.11/'\"" -DVAL_CC="\"gcc\"" -DVAL_CPPFLAGS="\"-D_GNU_SOURCE\"" -DVAL_CFLAGS="\"-Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2\"" -DVAL_CFLAGS_SL="\"-fPIC\"" -DVAL_LDFLAGS="\"-Wl,--as-needed -Wl,-rpath,'/pg_bin/pgsql/10.11/lib',--enable-new-dtags\"" -DVAL_LDFLAGS_EX="\"\"" -DVAL_LDFLAGS_SL="\"\"" -DVAL_LIBS="\"-lpgcommon -lpgport -lpthread -lz -lreadline -lrt -lcrypt -ldl -lm \""  -c -o base64.o base64.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -DFRONTEND -I../../src/include  -D_GNU_SOURCE  -DVAL_CONFIGURE="\"'--prefix=/pg_bin/pgsql/10.11/'\"" -DVAL_CC="\"gcc\"" -DVAL_CPPFLAGS="\"-D_GNU_SOURCE\"" -DVAL_CFLAGS="\"-Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2\"" -DVAL_CFLAGS_SL="\"-fPIC\"" -DVAL_LDFLAGS="\"-Wl,--as-needed -Wl,-rpath,'/pg_bin/pgsql/10.11/lib',--enable-new-dtags\"" -DVAL_LDFLAGS_EX="\"\"" -DVAL_LDFLAGS_SL="\"\"" -DVAL_LIBS="\"-lpgcommon -lpgport -lpthread -lz -lreadline -lrt -lcrypt -ldl -lm \""  -c -o config_info.o config_info.c
.
.
.
.
make[1]: Leaving directory `/tmp/postgresql-10.11/config'
All of PostgreSQL successfully made. Ready to install.

"All of PostgreSQL successfully made. Ready to install." Means all went well, we're good to install now.

Let’s run make install.

[root@demunix postgresql-10.11]# make install
[root@demunix postgresql-10.11]# make install
make -C src install
make[1]: Entering directory `/tmp/postgresql-10.11/src'
make -C common install
make[2]: Entering directory `/tmp/postgresql-10.11/src/common'
make -C ../backend submake-errcodes
.
.
.
make[1]: Entering directory `/tmp/postgresql-10.11/config'
/usr/bin/mkdir -p '/pg_bin/pgsql/10.11/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/pg_bin/pgsql/10.11/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/pg_bin/pgsql/10.11/lib/pgxs/config/missing'
make[1]: Leaving directory `/tmp/postgresql-10.11/config'
PostgreSQL installation complete.


"PostgreSQL installation complete." Means PostgreSQL Database successfully installed.

Let’s add user who will administer database server. And also set password.

[root@demunix postgresql-10.11]# useradd -d /home/postgres postgres
[root@demunix postgresql-10.11]# passwd postgres

Change the ownership of all four directories to postgres user.

[root@demunix postgresql-10.11]# cd
[root@demunix ~]# chown -R postgres:postgres /pg_bin/
[root@demunix ~]# chown -R postgres:postgres /pg_data/
[root@demunix ~]# chown -R postgres:postgres /backup/
[root@demunix ~]# chown -R postgres:postgres /archive/
[root@demunix ~]# chmod -R 750 /pg_bin/
[root@demunix ~]# chmod -R 750 /pg_data/
[root@demunix ~]# chmod -R 750 /backup/
[root@demunix ~]# chmod -R 750 /archive/

And permission to 750.

[root@demunix ~]# chmod -R 750 /pg_bin/
[root@demunix ~]# chmod -R 750 /pg_data/
[root@demunix ~]# chmod -R 750 /backup/
[root@demunix ~]# chmod -R 750 /archive/

Switch to postgres user and create .bash_profile to export environment variables.

[postgres@demunix ~]$ vi .bash_profile

#PostgreSQL Server Environment Variable Settings.

LD_LIBRARY_PATH=/pg_bin/pgsql/10.11/bin/
export LD_LIBRARY_PATH
PATH=/pg_bin/pgsql/10.11/bin/:$PATH
export PATH
PGDATA=/pg_data/pgsql/10.11/data
export PGDATA
PGDATABASE=demunix
export PGDATABASE
echo "PG Profile Loaded"
echo "Database Cluster directory is $PGDATA"
echo "PostgreSQL Version is 10.11"

Verify if environment variables are pointing at correct binary path.

[postgres@demunix ~]$ . .bash_profile 
PG Profile Loaded
Database Cluster directory is /pg_data/pgsql/10.11/data
PostgreSQL Version is 10.11
[postgres@demunix ~]$ psql -V
psql (PostgreSQL) 10.11
[postgres@demunix ~]$ pg_ctl -V
pg_ctl (PostgreSQL) 10.11

Let’s initialize database cluster using initdb

[postgres@demunix ~]$ initdb 
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".

Data page checksums are disabled.

fixing permissions on existing directory /pg_data/pgsql/10.11/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Kolkata
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

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

Success. You can now start the database server using:

    pg_ctl -D /pg_data/pgsql/10.11/data -l logfile start

Let’s start the database server.

[postgres@demunix ~]$ pg_ctl start
waiting for server to start....2020-03-26 17:20:30.836 IST [108220] LOG:  listening on IPv6 address "::1", port 5432
2020-03-26 17:20:30.836 IST [108220] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-03-26 17:20:30.851 IST [108220] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-03-26 17:20:30.864 IST [108221] LOG:  database system was shut down at 2020-03-26 17:19:32 IST
2020-03-26 17:20:30.866 IST [108220] LOG:  database system is ready to accept connections
 done
server started

Let’s invoke psql and create a database named demunix.

[postgres@demunix ~]$ psql
psql (10.11)
Type "help" for help.

postgres=# create database demunix;
CREATE DATABASE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 demunix   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

There you go. Peace 🙂