How to build and install PostgreSQL 18 beta 2 from Source code

How to build and install PostgreSQL 18 beta 2 from Source code

postgres_build_source_and_install

Here in this article we will try to build and install PostgreSQL database from source code package on Fedora 41 server.

Test Environment

  • Fedora 41 server

Postgres Database

PostgreSQL is an Open Source database developed at the University of California, Berkeley. It is an object-relational database management system (ORDBMS) based on POSTGRES, Version 4.2. It supports a large part of the SQL standard and offers many modern features. Also it can be extended by the user in many ways.

Procedure

Step1: Ensure Pre-requisite Packages installed

We need to ensure that the following software packages are installed which are required to build PostgreSQL.

Required Packages

admin@linuxscratch:~$ sudo dnf install make gcc
admin@linuxscratch:~$ sudo dnf install flex bison
admin@linuxscratch:~$ sudo dnf install perl
admin@linuxscratch:~$ sudo dnf install readline readline-devel icu libicu-devel

NOTE: Excluded zlib package due to issue during configure on Fedora 41 server

Step2: Download PostgreSQL Source Package

We can download the PostgreSQL source package from source download page. Let’s down 18beta2 version , extract the package and switch to extracted directory.

admin@linuxscratch:~$ wget https://ftp.postgresql.org/pub/source/v18beta2/postgresql-18beta2.tar.bz2
admin@linuxscratch:~$ tar -xvf postgresql-18beta2.tar.bz2
admin@linuxscratch:~$ cd postgresql-18beta2/

Step3: Configure Source tree

The configure script basically configures the source tree for your system and choose the options you would like. The script sets the default location where Postgres would be installed. Here we running the configure without zlib as its unable to detect the installed zlib package on Fedora 41 server.

admin@linuxscratch:~/postgresql-18beta2$ ./configure --without-zlib

Step4: Compile Source code

Here we will be compiling the source files to binary files.

admin@linuxscratch:~/postgresql-18beta2$ make

Step5: Install Binary

This will install files into the directories that were specified in the configure script. Make sure that you have

appropriate permissions to write into that area. Normally you need to do this step as root.

Also we are creating a user “postgres” and a data directory “/usr/local/pgsql/data” with ownership for the directory changed to “postgres”.

admin@linuxscratch:~/postgresql-18beta2$ sudo su -

root@linuxscratch:~# cd /home/admin/postgresql-18beta2/

root@linuxscratch:/home/admin/postgresql-18beta2# make install
root@linuxscratch:/home/admin/postgresql-18beta2# adduser postgres
root@linuxscratch:/home/admin/postgresql-18beta2# mkdir -p /usr/local/pgsql/data
root@linuxscratch:/home/admin/postgresql-18beta2# chown postgres /usr/local/pgsql/data

Step6: Initialize DB

Now its time to initialize our database service by running the “initdb” as user “postgres”.

root@linuxscratch:/home/admin/postgresql-18beta2# sudo su - postgres
postgres@linuxscratch:~$ /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".

Data page checksums are enabled.

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

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: 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:

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

Step7: Start Database Service

We are now ready to start our database service using the “pg_ctl” utility as shown below.

postgres@linuxscratch:~$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
waiting for server to start.... done
server started

Step8: Validate Database Service

Let’s now create a database and connect to it.

postgres@linuxscratch:~$ /usr/local/pgsql/bin/createdb test
postgres@linuxscratch:~$ /usr/local/pgsql/bin/psql test
psql (18beta2)
Type "help" for help.

test=# 

Step9: Stop Database Service

Once you have validated your database service you can shutodown the service as shown below.

postgres@linuxscratch:~$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile stop
waiting for server to shut down..... done
server stopped

Step10: Export PGSQL binary path

We can set the PGSQL binary path in “.bashrc” script for the “postgres” user so that we can call the binaries directly without providing the complete path.

postgres@linuxscratch:~$ cat ~/.bashrc 
...
export PATH=$PATH:/usr/local/pgsql/bin

Restart the shell terminal for the changes to take effect.

Hope you enjoyed reading this article. Thank you..