How to install and run PostgreSQL 18 from Source on Linux

PostgreSQL 18 is now officially released, introducing several internal improvements, performance enhancements, and architecture-level refinements. For developers, researchers, or database engineers who want to dive deeper into how PostgreSQL functions behind the scenes, compiling it from source is one of the most valuable learning paths. Unlike pre-built packages, source compilation exposes the entire build process, offering transparency into how each component is structured, linked, and executed inside the database engine.

Building PostgreSQL from source also provides complete control over debugging flags, assertion checks, compiler optimizations, and installation paths. This level of flexibility is essential for those who experiment with internal modules, add logging inside C source files, explore memory contexts, or work on custom patches and extensions. It becomes particularly beneficial for contributors who want to understand PostgreSQL’s execution flow or track how the planner, executor, and storage subsystems behave under different workloads.

This guide walks through every step required to install PostgreSQL 18 from source on a Linux system.

1. Preparing the Workspace

Start by creating a dedicated directory to keep your PostgreSQL source code organized:

mkdir postgres_source_code
cd postgres_source_code

Clone the official PostgreSQL repository and switch to the stable release branch for PostgreSQL 18:

git clone https://git.postgresql.org/git/postgresql.git -b REL_18_STABLE

Move into the cloned directory:

cd postgresql

2. Configuring PostgreSQL Source

Before building PostgreSQL, configure it with debugging options to make it easier to trace internal behavior:

../configure --prefix=$(pwd) --enable-cassert --enable-debug

The configuration options enable assertions and debugging symbols, which are crucial when studying how PostgreSQL executes commands internally.

3. Compiling PostgreSQL

Build PostgreSQL using all available CPU cores:

make -j $(nproc)

The nproc command shows the number of CPU cores in your system like this

cybrosys@cybrosys:nproc
16

You can also explore more details about this

cybrosys@cybrosys:nproc
16
You can also explore more details about this
cybrosys@cybrosys: nproc --help
Usage: nproc [OPTION]...
Print the number of processing units available to the current process,
which may be less than the number of online processors
      --all      print the number of installed processors
      --ignore=N  if possible, exclude N processing units
      --help     display this help and exit
      --version  output version information and exit
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/nproc>
or available locally via: info '(coreutils) nproc invocation'

Using -j $(nproc) speeds up compilation by running multiple jobs in parallel.

After the build completes, install PostgreSQL:

sudo make install

A bin directory will appear containing all PostgreSQL executables such as psql, pg_ctl, initdb, and various utilities.

4. Initializing a Database Cluster

PostgreSQL stores databases, metadata, WAL files, system catalogs, and configuration files inside a cluster directory. Initialize one using:

bin/initdb -D my_data

This command sets up a complete database directory, configures locale settings, prepares shared memory parameters, and generates default configuration files.

You can also see an output like this with the command for starting the postgres server.

cybrosys@cybrosys:~/postgres_18/postgresql$ bin/initdb -D myt_data-data
The files belonging to this database system will be owned by user "cybrosys".
This user must also own the server process.
The database cluster will be initialized with locale "en_IN".
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.
creating directory client-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:
    bin/pg_ctl -D my_data -l logfile start

5. Starting the PostgreSQL 18 Server

Start the server using pg_ctl:

bin/pg_ctl -D my_data -l logfile start

Connect to the server:

bin/psql postgres

Verify the server version and port:

select version();
show port;

It shows PostgreSQL 18.1 should now be running on its default port, 5432 like this

postgres=# select version();
                                                 version                                                 
---------------------------------------------------------------------------------------------------------
 PostgreSQL 18.1 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0, 64-bit
(1 row)
postgres=# show port;
 port 
------
 5432
(1 row)

6. Exploring PostgreSQL Built-In Help

PostgreSQL provides an internal help system accessible via psql:

\h
Eg : \h create index

These commands display syntax details and documentation references for SQL commands.

7. Running PostgreSQL on a Different Port

Stop the running PostgreSQL instance:

bin/pg_ctl -D my_data -l logfile stop

Create another cluster:

bin/initdb -D client-data

Start this cluster on a different port, such as 5433:

bin/pg_ctl -D client-data -l logfile -o '-p 5433' start

Connect to it:

bin/psql postgres -p 5433

Check the port:

show port;

This allows you to run multiple PostgreSQL servers simultaneously, each with its own data directory and configuration.

You can also show the path of postgres conf file inside psql terminal like this

postgres=# show config_file ;
                            config_file                            
-------------------------------------------------------------------
 /home/cybrosys/postgres_18/postgresql/client-data/postgresql.conf
(1 row)

You can also use bash commands inside psql terminal

Let's open this conf file inside postgres terminal by using this command

postgres=# \! sudo nano /home/cybrosys/postgres_18/postgresql/client-data/postgresql.conf

And also, you can monitor the postgres log file like this command

tail -f logfile

It shows output like this

cybrosys@cybrosys:~/postgres_18/postgresql$ tail -f logfile 
2025-11-22 10:47:25.327 IST [77490] LOG:  checkpoint starting: shutdown immediate
2025-11-22 10:47:25.336 IST [77490] LOG:  checkpoint complete: wrote 0 buffers (0.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=0.003 s, sync=0.002 s, total=0.010 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB; lsn=0/1758A78, redo lsn=0/1758A78
2025-11-22 10:47:25.344 IST [77486] LOG:  database system is shut down
2025-11-22 10:47:35.205 IST [77735] LOG:  starting PostgreSQL 18.1 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0, 64-bit
2025-11-22 10:47:35.205 IST [77735] LOG:  listening on IPv4 address "127.0.0.1", port 5433
2025-11-22 10:47:35.207 IST [77735] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5433"
2025-11-22 10:47:35.212 IST [77741] LOG:  database system was shut down at 2025-11-22 10:47:25 IST
2025-11-22 10:47:35.215 IST [77735] LOG:  database system is ready to accept connections
2025-11-22 10:52:35.311 IST [77739] LOG:  checkpoint starting: time
2025-11-22 10:52:39.536 IST [77739] LOG:  checkpoint complete: wrote 42 buffers (0.3%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=4.214 s, sync=0.005 s, total=4.225 s; sync files=11, longest=0.003 s, average=0.001 s; distance=319 kB, estimate=319 kB; lsn=0/17A8840, redo lsn=0/17A87B0

Conclusion

Compiling PostgreSQL from source opens the door to understanding how the database engine works beneath the surface. With PostgreSQL 18, the process remains clean and straightforward: clone the source code, configure it with debugging options, compile using all CPU cores, initialize a cluster, and start the server on any port you choose.

This method is especially valuable for developers who need deeper visibility into PostgreSQL internals, teams experimenting with extensions or custom patches, and researchers studying query execution, memory contexts, and the storage engine. Running multiple clusters on different ports also makes it easier to test changes without affecting production environments.

By following the steps outlined above, you not only install PostgreSQL 18 successfully but also set up a flexible development environment that supports exploration, debugging, and performance analysis.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message