Installing PostgreSQL using Source Code
By: Manish Sharma
Tags: Postgres, Python, JavaScript, Database, Sql

This is a guide for beginners and also a memo for myself, if I forget how to install PostgreSQL using source code in future. So lets begin.

Ever since I stared learning server side programming, I have seen other developers use Postgresql for almost all of their projects. Because it is the best open-source relational database for most projects, whether it is development or production.

What’s most tedious is the installation process of the postgres. You might find the documentation confusing unless you have experience compiling source codes using make .

Basically this guide is for those beginners who started their projects in Javascript or Python , and want to replace their Sqlite3 or mongodb databsase with PostgreSQL. This guide considers that you are using some distribution of linux, and you can follow along even if you are a mac user.

Download the latest Source file:

Example version: 13.1

https://www.postgresql.org/ftp/source/<version>/postgresql-<version>.tar.gz

Extract the downloaded file:

tar xf postgresql-<version>.tar.gz

cd into the extracted directory:

cd postgresql-<version>

Now configure the source:

./configure --prefix=<absolute-path-to-this-directory> --without-readline --without-zlib

Example: ./configure --prefix=/home/user/Downloads/postgresql-13.1/ --without-readline --without-zlib

--prefix=DIRNAME << Install all files under the directory PREFIX instead of /usr/local/pgsql .

The actual files will be installed into various sub-directories; no files will ever be installed directly into the PREFIX directory.

--without-readline << Prevents use of the Readline library (and libedit as well). This option disables command-line editing and history in psql .

--without-zlib << Prevents use of the Zlib library. This disables support for compressed archives in pg_dump and pg_restore.

(you can omit those flags if you want)

Once you configure using the above command, the postgres configure program will run some checks on your system for compatibility and hardware specs.

After configure, start make to compile the postgres:

make

Now this will usually take ≥15 minutes depending on your system configurations. So make yourself a coffee, or probably just for fun go through postgresql’s docs for postgresql queries and commands.

After source is compiled, install the postgresql.

But before that, make sure you check the compilation:

make check

Hopefully everything should be good, then install:

make install

This won’t take as long as make did.

Now we should have postgres installed.

So lets check.

Make a folder called data inside the /postgresql-<version> directory

mkdir data

and initialize the data directory:

su postgres -c “./bin/pg_ctl initdb -D data”

this will prompt for a password to be set, set your favourite password

Now we must start the postgres server so your applications can connect to it

type this to start

su postgres -c “./bin/pg_ctl start -D data”

(reuse the command and replace start with stop , to stop the server)

you can customize the port by passing -port=<port> (default is 5432 )

Enter the PSQL through shell:

su postgres -c “./bin/psql”

You now will be able to interact with the postgres database.

Remember the coffee break, where I asked you to go through the useful queries and commands, this is where its useful.

But, if you just drank the coffee, here’s a gist full of basic commands, PostgreSQL

Finally, you can quit the psql shell using \q ,

read help docs using \h

Ok Bye.