DEV Community

Cover image for Installing PostgreSQL FROM Source Code (Full Installation Guide)
Nnaemeka Daniel John
Nnaemeka Daniel John

Posted on

Installing PostgreSQL FROM Source Code (Full Installation Guide)

Hey there, tech enthusiasts! Today, we're diving into the exciting world of PostgreSQL and its full installation from source code. PostgreSQL, also known as Postgres, is a powerful open-source relational database management system that has earned its reputation for robustness, reliability, and extensibility. While installing PostgreSQL from source code might sound a bit daunting, fear not! I've got you covered with this very easy-to-follow guide.

Gather the Prerequisites

Before we begin, we need to ensure that we have all the necessary tools and libraries on our system:

  • C Compiler: PostgreSQL is written in C, so a C compiler is a must. Popular choices include GCC (GNU Compiler Collection) and Clang.

  • Build Tools: Ensure you have essential build tools like make and autoconf installed. These tools help in compiling the source code efficiently.

  • Libraries: PostgreSQL requires certain libraries like readline and zlib. We need to install these development libraries on our system.

Depending on your operating system this is the command used to install the needed dependencies on Ubuntu;
sudo apt-get install build-essential libreadline-dev zlib1g-dev flex bison.

Download the Source Code

Head over to the official PostgreSQL website PostgreSQL Source Download and download the source code of the desired version. If you intend to use the Apache AGE extension with PostgreSQL, it is advisable to download version 11, 12 or 13 which AGE currently support.

Extract the Source Code

Once the download is complete, navigate to the directory where the source code is saved and extract it using the following command:

tar xvfz postgresql-13.10.tar.gz
Enter fullscreen mode Exit fullscreen mode

Configure PostgreSQL

Once you're done extracting, navigate into the extracted directory, and run configure.

cd postgresql-13.10
Enter fullscreen mode Exit fullscreen mode

The configuration step ensures that PostgreSQL is tailored to your system. I'll recommend a basic configuration to get started:

./configure --prefix=$(pwd) --enable-cassert --enable-debug
Enter fullscreen mode Exit fullscreen mode
  • The --prefix install all files under the directory PREFIX instead of /usr/local/pgsql. In this case, our present working directory.

  • The --enable-debug compiles all programs and libraries with debugging symbols. This means that you can run the programs in a debugger to analyze problems.

  • The --enable-cassert enables assertion checks in the server, which test for many “cannot happen” conditions. This option is not recommended for production use, but you should have it on for development work or when running a beta version.

Compile and Install

Now, let's compile the source code and install PostgreSQL on our system.

gmake && gmake install
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can use make && make install.
The gmake command will compile the source code, and gmake install will install PostgreSQL. You can use sudo to ensure that the installation has the required permissions.

Initialize the Database Cluster

After the installation, right in our postgres directory:

user@ubuntu:/home/postgresql-13.10$ bin/initdb my_db
Enter fullscreen mode Exit fullscreen mode

You can use the bin/initdb my_db command to initialize a database cluster named my_db.

Start PostgreSQL Service

We can now start the PostgreSQL service using;

user@ubuntu:/home/postgresql-13.10$ bin/pg_ctl -D my_db -l logfile start
Enter fullscreen mode Exit fullscreen mode

By default, the postgres server runs on port 5432, but you can change that in the postgresql.conf file.

Create a PostgreSQL Database

Once our server is started, we can now create a new database using the command below;

user@ubuntu:/home/postgresql-13.10$ bin/createdb --port=5432 new_db
Enter fullscreen mode Exit fullscreen mode

The above command creates a new database called new_db in our database cluster my_db.

And you can start writing queries by connecting to the database;

user@ubuntu:/home/postgresql-13.10$ bin/psql --port=5432 my_db
my_db=# \dt
my_db=# \q
Enter fullscreen mode Exit fullscreen mode

You can stop the database server using the command;

user@ubuntu:/home/postgresql-13.10$ bin/pg_ctl -D my_db stop
Enter fullscreen mode Exit fullscreen mode

Congratulations! You have successfully installed PostgreSQL from source code.

Conclusion

You've accomplished an impressive feat - installing PostgreSQL from source code! Embracing the power of PostgreSQL allows you to handle complex data needs and build robust applications. Remember, the journey of a thousand queries begins with a single installation, and you're now well-equipped to harness the potential of this incredible database management system.

So, take charge, explore the vast PostgreSQL documentation, and let your data-driven dreams soar! Happy coding!

References

Top comments (0)