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.
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.
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.
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
Once you're done extracting, navigate into the extracted directory, and run configure.
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
--prefixinstall all files under the directory PREFIX instead of /usr/local/pgsql. In this case, our present working directory.
--enable-debugcompiles all programs and libraries with debugging symbols. This means that you can run the programs in a debugger to analyze problems.
--enable-cassertenables 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.
Now, let's compile the source code and install PostgreSQL on our system.
gmake && gmake install
Alternatively, you can use
make && make install.
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.
After the installation, right in our postgres directory:
user@ubuntu:/home/postgresql-13.10$ bin/initdb my_db
You can use the
bin/initdb my_db command to initialize a database cluster named
We can now start the PostgreSQL service using;
user@ubuntu:/home/postgresql-13.10$ bin/pg_ctl -D my_db -l logfile start
By default, the postgres server runs on port 5432, but you can change that in the
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
The above command creates a new database called
new_db in our database cluster
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
You can stop the database server using the command;
user@ubuntu:/home/postgresql-13.10$ bin/pg_ctl -D my_db stop
Congratulations! You have successfully installed PostgreSQL from source code.
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!