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

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

Firefox browser is the default browser for most modern Linux distributions such as Ubuntu, Mint, and Fedora. Initially, its performance might be impressive, however, with the passage of time, you might notice that your browser is not as fast and resp

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

If you find that the server is running slowly or the memory usage is too high, you should check the cause before operating. First, you need to check the system resource usage, use top, htop, free-h, iostat, ss-antp and other commands to check CPU, memory, disk I/O and network connections; secondly, analyze specific process problems, and track the behavior of high-occupancy processes through tools such as ps, jstack, strace; then check logs and monitoring data, view OOM records, exception requests, slow queries and other clues; finally, targeted processing is carried out based on common reasons such as memory leaks, connection pool exhaustion, cache failure storms, and timing task conflicts, optimize code logic, set up a timeout retry mechanism, add current limit fuses, and regularly pressure measurement and evaluation resources.

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

Frankly speaking, I cannot recall the last time I used a PC with a CD/DVD drive. This is thanks to the ever-evolving tech industry which has seen optical disks replaced by USB drives and other smaller and compact storage media that offer more storage

In Linux systems, 1. Use ipa or hostname-I command to view private IP; 2. Use curlifconfig.me or curlipinfo.io/ip to obtain public IP; 3. The desktop version can view private IP through system settings, and the browser can access specific websites to view public IP; 4. Common commands can be set as aliases for quick call. These methods are simple and practical, suitable for IP viewing needs in different scenarios.

Built on Chrome’s V8 engine, Node.JS is an open-source, event-driven JavaScript runtime environment crafted for building scalable applications and backend APIs. NodeJS is known for being lightweight and efficient due to its non-blocking I/O model and
