Introduction
PostgreSQL is a powerful open-source relational database management system (RDBMS) known for its stability, extensibility, and compliance with SQL standards. In this guide, we will explore two methods for installing PostgreSQL: using package managers and building from source code.
PRE – Requisite: PGDG Repository – PostgreSQL Global Development Group Repository
When we install Actual PostgreSQL Software it will automatically bring/download it from PGDG Repository for that Before Actual PostgreSQL Software installation we have to install PGDG Repository Package
To Check PGDG Repository has been installed or not
Create Postgres User:
1 2 3 4 5 6 |
sudo mkdir -p /var/lib/postgresql && \ sudo chown -R postgres:postgres /var/lib/postgresql && \ sudo chmod 700 /var/lib/postgresql && \ sudo groupadd postgres_group |
1 2 3 4 5 6 7 |
sudo dnf -y list installed |grep pgdg-redhat or sudo dnf -y repolist or ls -l /etc/yum.repos.d/pgdg* |
1 2 3 4 |
# Install the repository RPM(Redhat Package Manager - Package): sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm |
Method 1: Installing PostgreSQL using Package Managers
Package managers simplify the installation process by handling dependencies and providing easy updates. Here are the steps to install PostgreSQL using popular package managers:
1. Ubuntu / Debian
To install PostgreSQL on Ubuntu or Debian, open a terminal and run the following commands:
1 2 3 4 |
sudo apt update sudo apt install postgresql |
2. CentOS / Fedora
For CentOS or Fedora, use the following commands:
1 2 3 4 5 6 |
sudo dnf list installed |grep postgresql16 sudo dnf install -y postgresql16-server sudo systemctl enable --now postgresql sudo postgresql-setup initdb |
Method 2: Building PostgreSQL from Source Code
Building from source code allows for more customization and control over the installation. Here are the steps to build PostgreSQL:
1. Install Dependencies
Before compiling PostgreSQL, ensure that you have the necessary dependencies installed. Refer to the PostgreSQL documentation for the specific requirements based on your operating system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#For Centos, RHEL and Fedora. sudo dnf install -y gcc make autoconf automake libtool sudo dnf install -y readline-devel zlib-devel openssl-devel libxml2-devel #For Debian and Ubuntu sudo apt update sudo apt install -y build-essential sudo apt install -y libreadline-dev zlib1g-dev libssl-dev libxml2-dev #create postgres User If Not Available: sudo useradd postgres #change the password of the user: sudo passwd postgres #create the necessary directories Needed: sudo mkdir -p /u01/PostgreSQL/16.1 sudo chown -R postgres:postgres /u01/PostgreSQL/ sudo chmod -R 700 /u01/PostgreSQL/ |
2. Download the Source Code
Visit the official PostgreSQL website (https://www.postgresql.org/) and download the latest stable release. Extract the downloaded archive to a directory of your choice.
1 2 3 4 5 6 |
wget https://ftp.postgresql.org/pub/source/v16.1/postgresql-16.1.tar.gz ls -lh postgresql-16.1.tar.gz tar -xvf postgresql-16.1.tar.gz du -sh /var/lib/pgsql/postgresql-16.1 |
3. Configure and Build
In the terminal, navigate to the extracted PostgreSQL source code directory and run the following commands:
Default Installation Path:
1 2 3 4 5 |
./configure make sudo make install |
Custom Installation Path:
1 2 3 4 5 6 7 8 9 10 11 |
---prechecks---instead of defualt path to be installed(/usr/local/pgsql) ./configure --prefix=/u01/PostgreSQL/16.1 --build the software--- make world ---actual installation--- make install-world |
4. Initialize the Database
After installation, initialize the PostgreSQL database cluster by running:
1 2 3 4 5 6 7 8 9 10 11 12 |
/u01/PostgreSQL/16.1/bin/initdb -D /u01/PostgreSQL/16.1/data #OR #Export the environment Variables and start like this. export PATH=/u01/PostgreSQL/16.1/bin:$PATH export PGDATA=/u01/PostgreSQL/16.1/data export PGPORT=5433 #Then Trigger: initdb |
5. Start PostgreSQL
To start the PostgreSQL server, run:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#To Start the Database /u01/PostgreSQL/16.1/bin/pg_ctl -D /u01/PostgreSQL/16.1/data start #To Check Status /u01/PostgreSQL/16.1/bin/pg_ctl -D /u01/PostgreSQL/16.1/data start #OR export PATH=/u01/PostgreSQL/16.1/bin:$PATH export PGDATA=/u01/PostgreSQL/16.1/data export PGPORT=5433 #Then Trigger: pg_ctl status pg_ctl start pg_ctl status |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
postgres@classicdba_pgmaster:/u01/PostgreSQL/16.1/data$ export PATH=/u01/PostgreSQL/16.1/bin:$PATH export PGDATA=/u01/PostgreSQL/16.1/data export PGPORT=5433 postgres@classicdba_pgmaster:/u01/PostgreSQL/16.1/data$ pg_ctl status pg_ctl: server is running (PID: 28310) /u01/PostgreSQL/16.1/bin/postgres "-D" "/u01/PostgreSQL/16.1/data" postgres@classicdba_pgmaster:/u01/PostgreSQL/16.1/data$ 2024-02-27 02:47:15.526 UTC [28311] LOG: checkpoint starting: time 2024-02-27 02:47:19.648 UTC [28311] LOG: checkpoint complete: wrote 44 buffers (0.3%); 0 WAL file(s) added, 0 removed, 0 recycled; write=4.115 s, sync=0.003 s, total=4.122 s; sync files=11, longest=0.002 s, average=0.001 s; distance=256 kB, estimate=256 kB; lsn=0/14C0A60, redo lsn=0/14C0A28 |
Method 3 : Manual Download Packages based PostgreSQL Software
Before this verify/checks your currently using Operating System Version for that use the command
1 2 3 4 5 |
[postgres@classicdba_pgmaster ~]$ cat /etc/system-release Oracle Linux Server release 8.9 (In this its Oracle Linux Version 8 , Oracle Linux update 9) [postgres@classicdba_pgmaster ~]$ |
Goto : https://yum.postgresql.org/
—>Select the Major version of PostgreSQL Software
—>Select the OS Version
—>
Download the version of your choice.
Lets Try Downloading and installing the older version of postgresql.
1 2 3 4 5 6 |
wget https://download.postgresql.org/pub/repos/yum/13/redhat/rhel-8-x86_64/postgresql13-contrib-13.0-1PGDG.rhel8.x86_64.rpm wget https://download.postgresql.org/pub/repos/yum/13/redhat/rhel-8-x86_64/postgresql13-server-13.0-1PGDG.rhel8.x86_64.rpm wget https://download.postgresql.org/pub/repos/yum/13/redhat/rhel-8-x86_64/postgresql13-13.0-1PGDG.rhel8.x86_64.rpm wget https://download.postgresql.org/pub/repos/yum/13/redhat/rhel-8-x86_64/postgresql13-libs-13.0-1PGDG.rhel8.x86_64.rpm |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
sudo rpm -ivh *.rpm [postgres@classicdba_pgmaster ~]$ ls postgresql13-13.0-1PGDG.rhel8.x86_64.rpm postgresql13-server-13.0-1PGDG.rhel8.x86_64.rpm postgresql13-contrib-13.0-1PGDG.rhel8.x86_64.rpm postgresql-15.6 postgresql13-libs-13.0-1PGDG.rhel8.x86_64.rpm postgresql-15.6.tar.gz [postgres@classicdba_pgmaster ~]$ sudo rpm -ivh *.rpm Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:postgresql13-libs-13.0-1PGDG.rhel################################# [ 25%] 2:postgresql13-13.0-1PGDG.rhel8 ################################# [ 50%] 3:postgresql13-server-13.0-1PGDG.rh################################# [ 75%] 4:postgresql13-contrib-13.0-1PGDG.r################################# [100%] [postgres@classicdba_pgmaster ~]$ |
Conclusion
PostgreSQL can be installed using package managers or by building from source code. Package managers provide a straightforward installation process, while building from source code allows for more customization. Choose the method that best suits your needs and enjoy the power and reliability of PostgreSQL for your database management.