PostgreSQL, an open-source relational database management system, is highly valued for its powerful features and adaptability. Although many Linux distributions offer PostgreSQL via their package managers, building it from source gives users more personalization and control.
This article will guide you through the process of installing PostgreSQL 16 using source code on Linux systems.
For those preferring a simpler installation method via the package manager, refer to the following guides:
- How to Install PostgreSQL 16 on RHEL-Based Systems
- How to Install PostgreSQL 16 and pgAdmin on Debian 12
Prerequisites
Before proceeding with the PostgreSQL installation, confirm that your system satisfies the following prerequisites:
- A Linux distribution (this guide uses Debian for illustration).
- A Linux system with a non-root user possessing sudo privileges.
- Essential development tools like GCC and Make must be installed.
Install Prerequisites on Linux
Initially, install necessary development tools such as GCC and Make using the distribution's package manager as indicated.
On RHEL-based distributions like CentOS, Fedora, Rocky Linux, and Alma Linux:
<code>sudo yum groupinstall development-tools sudo yum install zlib-devel readline-devel libicu-devel</code>
On Debian-based distributions such as Ubuntu and Linux Mint:
<code>sudo apt install gcc build-essential zlib1g-dev libreadline6-dev libicu-dev pkg-config</code>
Download the PostgreSQL Source Code
With the prerequisites in place, download the source code tar file from the official PostgreSQL website using the following wget command directly on the system. At the time of writing, the latest version is PostgreSQL 16.1.
<code>wget https://ftp.postgresql.org/pub/source/v16.1/postgresql-16.1.tar.bz2</code>
Next, use the tar command to extract the downloaded tarball file. A new directory named postgresql-16.1 will be created.
<code>tar -xvf postgresql-16.1.tar.bz2 cd postgresql-16.1/ ls -l</code>
Sample Output:
<code>-rw-r--r--. 1 tecmint tecmint 365 Nov 7 03:34 aclocal.m4 drwxr-xr-x. 2 tecmint tecmint 4096 Nov 7 03:47 config -rwxr-xr-x. 1 tecmint tecmint 584560 Nov 7 03:34 configure -rw-r--r--. 1 tecmint tecmint 87292 Nov 7 03:34 configure.ac drwxr-xr-x. 61 tecmint tecmint 4096 Nov 7 03:47 contrib -rw-r--r--. 1 tecmint tecmint 1192 Nov 7 03:34 COPYRIGHT drwxr-xr-x. 3 tecmint tecmint 4096 Nov 7 03:47 doc -rw-r--r--. 1 tecmint tecmint 4288 Nov 7 03:34 GNUmakefile.in -rw-r--r--. 1 tecmint tecmint 277 Nov 7 03:34 HISTORY -rw-r--r--. 1 tecmint tecmint 64601 Nov 7 03:48 INSTALL -rw-r--r--. 1 tecmint tecmint 1875 Nov 7 03:34 Makefile -rw-r--r--. 1 tecmint tecmint 102017 Nov 7 03:47 meson.build -rw-r--r--. 1 tecmint tecmint 6266 Nov 7 03:34 meson_options.txt -rw-r--r--. 1 tecmint tecmint 1213 Nov 7 03:34 README drwxr-xr-x. 16 tecmint tecmint 4096 Nov 7 03:48 src</code>
Configure PostgreSQL from Source
Since postgres is an open-source database, it can be compiled from source code based on individual needs. You can customize the build and installation process by providing one or more command-line options for various optional features.
Use the following command for help regarding available options and configuration usage, as shown.
<code>./configure --help</code>
Now run the configure script, which will check your system for dependencies and configure the build accordingly.
<code>./configure</code>
Install PostgreSQL from Source
Once configured, use the following commands to compile and install PostgreSQL from the source.
<code>make sudo make install</code>
Create Postgres User
Create a postgres user and directory to serve as the data directory for initializing the database cluster. The owner of this data directory should be the postgres user, and permissions should be set to 700. Additionally, set a path for the PostgreSQL binaries for convenience.
<code>sudo useradd postgres sudo passwd postgres sudo mkdir -p /pgdatabase/data sudo chown -R postgres: /pgdatabase/data sudo sh -c "echo 'export PATH=$PATH:/opt/PostgreSQL/bin' > /etc/profile.d/postgres.sh" source /etc/profile.d/postgres.sh </code>
Initialize Postgres Database
Now initialize the database using the following command as the postgres user before using any postgres commands.
<code>su postgres initdb -D /pgdatabase/data/ -U postgres -W</code>
Where -D
specifies the location for this database cluster or the data directory where you want to initialize the database cluster, -U
specifies the database superuser name, and -W
prompts for the superuser password.
For more information and options, refer to initdb --help
.
Start PostgreSQL Service
After initializing the database, start the database cluster. If you need to modify the port or specify the listening address for the server, edit the /pgdatabase/data/postgresql.conf file in the data directory of the database server.
<code>nano /pgdatabase/data/postgresql.conf</code>
Now, start the PostgreSQL service.
<code>pg_ctl -D /pgdatabase/data/ start</code>
After starting the database, verify the status of the postgres server process by using the following ps and netstat commands.
<code>ps -ef |grep -i postgres netstat -apn |grep -i 51751</code>
We can see that the database cluster is running smoothly, and startup logs can be found at the location specified with the -l
option while starting the database cluster.
<code>pg_ctl -D /pgdatabase/data/ -l logfile start</code>
Connect to PostgreSQL
Now connect to the database cluster and create a database by using the following commands.
<code>psql -p 5432 <strong>postgres=#</strong> create database test; <strong>postgres=#</strong> \l to list all databases in cluster <strong>postgres=#</strong> \q to quit from postgres console</code>
If you prefer a graphical tool called pgAdmin to manage your PostgreSQL, follow these guides to install pgAdmin on your Linux
The above is the detailed content of How to Install PostgreSQL Using Source Code in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Are you looking for good software to write mathematical equations? If so, this article provides the top 5 equation editors that you can easily install on your favorite Linux distribution.In addition to being compatible with different types of mathema

Linux administrators should be familiar with the command-line environment. Since GUI (Graphical User Interface) mode in Linux servers is not commonly installed.SSH may be the most popular protocol to enable Linux administrators to manage the servers

Gogo is a remarkable tool to bookmark directories inside your Linux shell. It helps you create shortcuts for long and complex paths in Linux. This way, you no longer need to type or memorize lengthy paths on Linux.For example, if there's a directory

PPA is an important tool for Ubuntu users to expand their software sources. 1. When searching for PPA, you should visit Launchpad.net, confirm the official PPA in the project official website or document, and read the description and user comments to ensure its security and maintenance status; 2. Add PPA to use the terminal command sudoadd-apt-repositoryppa:/, and then run sudoaptupdate to update the package list; 3. Manage PPAs to view the added list through the grep command, use the --remove parameter to remove or manually delete the .list file to avoid problems caused by incompatibility or stopping updates; 4. Use PPA to weigh the necessity and prioritize the situations that the official does not provide or require a new version of the software.

LXD is described as the next-generation container and virtual machine manager that offers an immersive for Linux systems running inside containers or as virtual machines. It provides images for an inordinate number of Linux distributions with support

How to quickly generate test files of a specified size? It can be achieved using command line tools or graphical software. On Windows, you can use fsutilfilecreatenew file name size to generate a file with a specified byte; macOS/Linux can use ddif=/dev/zeroof=filebs=1Mcount=100 to generate real data files, or use truncate-s100M files to quickly create sparse files. If you are not familiar with the command line, you can choose FSUtilGUI, DummyFileGenerator and other tool software. Notes include: pay attention to file system limitations (such as FAT32 file size upper limit), avoid overwriting existing files, and some programs may

Node Version Manager (NVM) is a simple bash script that helps manage multiple Node.js versions on your Linux system. It enables you to install various Node.js versions, view available versions for installation, and check already installed versions.NV

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.
