DEV Community

Yuya Shinde
Yuya Shinde

Posted on

1

Building and Running PostgreSQL from Source Code

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Finally, connect to the PostgreSQL database using psql command:

${PG_DIR_PREFIX}/bin/psql -d postgres
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Step 7: Disconnect from PostgreSQL

Enter \q to exit.

postgres=# \q
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

That's it! In my next article, I'll explore how to debug the PostgreSQL process.

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)