In this article, I'll walk you through the steps to build and run PostgreSQL database server on Linux machine.
Preface
- This article is intended for use on Debian-based Linux systems.
- The steps is intended for PostgreSQL development. For general use, I recommend installing a pre-compiled package from your Linux distribution's repository.
Step 1: Install Git and Clone the PostgreSQL Repository
The first step is making sure that Git is installed on your system. If it's not already installed, you can install it using the following command:
sudo apt-get install git
Next, clone the official PostgreSQL repository. Note: You can clone the repository anywhere; I'm using ~/Workplace
in this article.
mkdir -p ~/Workplace
cd Workplace/
git clone git://git.postgresql.org/git/postgresql.git
Step 2: Install Required Packages
Before building PostgreSQL database server, you need to install some required packages.
sudo apt-get install -y build-essential git gdb lcov bison flex \
libkrb5-dev libssl-dev libldap-dev libpam0g-dev python3-dev \
tcl-dev libperl-dev gettext libxml2-dev libxslt1-dev \
libreadline-dev libedit-dev uuid-dev libossp-uuid-dev \
libipc-run-perl perl
The configure
script running in the next step will check for these packages and prompt you to install them if you are missing any other packages.
Step 3: Build and Install PostgreSQL
Now it's time to build and install PostgreSQL. Create a new directory for the build files, set the PG_DIR_PREFIX
environment variable and set it as a value of the --prefix
option. You can set any prefix location, but avoid building inside the cloned repository directory for better organization.
We also enable debugging and disable optimization to prepare for GDB debugging in the next step.
cd postgresql
mkdir -p ${HOME}/Workplace/build/pgsql/master/
export PG_DIR_PREFIX=${HOME}/Workplace/build/pgsql/master
./configure --prefix=${PG_DIR_PREFIX} --enable-depend --enable-debug --enable-cassert --enable-tap-tests CFLAGS=-O0
make
make install
Step 4: Initialize and Start PostgreSQL
After building and installing PostgreSQL, initialize the database and start the server:
mkdir -p ${PG_DIR_PREFIX}/data
${PG_DIR_PREFIX}/bin/initdb -D ${PG_DIR_PREFIX}/data
${PG_DIR_PREFIX}/bin/pg_ctl -D ${PG_DIR_PREFIX}/data -l logfile start
Step 5: Verify that PostgreSQL is Running
To verify that PostgreSQL server is running, check the process list:
ps -ef | grep postgres
yuyash 54508 1 0 11:32 ? 00:00:00 /home/yuyash/Workplace/build/pgsql/master/bin/postgres -D /home/yuyash/Workplace/build/pgsql/master/data
yuyash 54509 54508 0 11:32 ? 00:00:00 postgres: checkpointer
yuyash 54510 54508 0 11:32 ? 00:00:00 postgres: background writer
yuyash 54512 54508 0 11:32 ? 00:00:00 postgres: walwriter
yuyash 54513 54508 0 11:32 ? 00:00:00 postgres: autovacuum launcher
yuyash 54514 54508 0 11:32 ? 00:00:00 postgres: logical replication launcher
yuyash 54524 39226 0 11:32 pts/2 00:00:00 grep --color=auto postgres
Step 6: Connect to PostgreSQL using psql
Before using psql
command, set shared library path:
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${PG_DIR_PREFIX}/lib
Finally, connect to the PostgreSQL database using psql
command:
${PG_DIR_PREFIX}/bin/psql -d postgres
To test the connection, execute a simple query to retrieve the version number:
postgres=# select version();
version
----------------------------------------------------------------------------------------------------
PostgreSQL 18devel on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
(1 row)
Step 7: Disconnect from PostgreSQL
Enter \q
to exit.
postgres=# \q
Step 8: Stop PostgreSQL
To stop the PostgreSQL server, execute following command:
${PG_DIR_PREFIX}/bin/pg_ctl stop -D ${PG_DIR_PREFIX}/data -m smart
That's it! In my next article, I'll explore how to debug the PostgreSQL process.
Top comments (0)