Magazine

How to Install PostgreSQL from Source on Linux

Posted on the 04 May 2023 by Top10

PostgreSQL, also called Postgres, is a powerful open source object-relational database system. It is an enterprise-grade database with features such as write-ahead logging for fault tolerance, asynchronous replication, multi-version concurrency control (MVCC), online backup/hot backup, point recovery, query scheduler/optimizer, tablespaces, nested transactions (savepoints), etc.

The latest version of Postgres 15.2 was released on February 9, 2023 by the global PostgreSQL development team.

Features of PostgreSQL

The features of the new version are as follows:

  • Logical Replication: This feature allows you to replicate individual database objects (whether rows, tables, or selective databases) on standby servers. It provides more control over data replication. Implemented using a publisher-subscriber model.
  • Quorum Commit for synchronous replication: In this function, dba can now specify the number of standby servers that confirm changes to the database have been made so that the data can be considered securely written.
  • SCRAM-SHA-256 authentication: Improved security over existing MD5-based authentication and password storage.
  • Improved parallel query execution.
  • Declarative table partitioning.
  • Full text search support for JSON and JSONB.

In this article, we'll show you how to install PostgreSQL 15 using install from source on Linux systems. For those looking for a simple installation from the distribution's package manager, we looked at installing PostgreSQL 15 on Rocky Linux and AlmaLinux:

How to install PostgreSQL 15 on Rocky Linux and AlmaLinux

Installing PostgreSQL from source

Because postgres is an open source database, it can be built from source to suit your needs. We can customize the build and installation process by adding one or more command line options for various additional features.

The main advantage of using the source installation is that it can be largely customized during the installation process.

1. First, install the required prerequisites such as gcc, readline-devel and zlib-devel using the package manager as shown in the figure.

# yum install gcc zlib-devel readline-devel     [On RHEL/CentOS]
# apt install gcc zlib1g-dev libreadline6-dev   [On Debian/Ubuntu]

2. Download the source code tar file from the official postgres website using the following wget command directly on the system.

# wget

3. Use the tar command to extract the downloaded tarball file. A new directory named postgresql-15.2 will be created.

# tar -xvf postgresql-15.2.tar.bz2
# cd postgresql-15.2
# ls -l

sample output

total 780
-rw-r--r--  1 1107 1107    397 Feb  6 16:39 aclocal.m4
drwxrwxrwx  2 1107 1107   4096 Feb  6 16:50 config
-rwxr-xr-x  1 1107 1107 601519 Feb  6 16:39 configure
-rw-r--r--  1 1107 1107  89258 Feb  6 16:39 configure.ac
drwxrwxrwx 61 1107 1107   4096 Feb  6 16:50 contrib
-rw-r--r--  1 1107 1107   1192 Feb  6 16:39 COPYRIGHT
drwxrwxrwx  3 1107 1107     87 Feb  6 16:50 doc
-rw-r--r--  1 1107 1107   4264 Feb  6 16:39 GNUmakefile.in
-rw-r--r--  1 1107 1107    277 Feb  6 16:39 HISTORY
-rw-r--r--  1 1107 1107  63842 Feb  6 16:51 INSTALL
-rw-r--r--  1 1107 1107   1875 Feb  6 16:39 Makefile
-rw-r--r--  1 1107 1107   1213 Feb  6 16:39 README
drwxrwxrwx 16 1107 1107   4096 Feb  6 16:51 src

4. The next step in the installation procedure is to configure the downloaded source code by selecting options according to your needs. Use ./configure -help to get help on various options.

# ./configure --help

`configure' configures PostgreSQL 15.2 to adapt to many kinds of systems. 

Usage: ./configure [OPTION]... [VAR=VALUE]...
 
To assign environment variables (e.g., CC, CFLAGS...), specify them asVAR=VALUE.  See below for descriptions of some of the useful variables.
 
Defaults for the options are specified in brackets.
 
Configuration:
  -h, --help              display this help and exit
       --help=short        display options specific to this package
       --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
       --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
       --srcdir=DIR        find the sources in DIR [configure dir or `..']
 
Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local/pgsql]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]
....

5. Now create the directory where you want to install the postgres files and use the prefix option in configure.

# mkdir /opt/PostgreSQL# ./configure --prefix=/opt/PostgreSQL

sample output

checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for gcc option to accept ISO C99... none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for gawk... gawk
checking whether gcc supports -Wdeclaration-after-statement, for CFLAGS... yes
checking whether gcc supports -Werror=vla, for CFLAGS... yes
checking whether gcc supports -Werror=unguarded-availability-new, for CFLAGS... no....

Building PostgreSQL from source

6. Once configured, we will start building PostgreSQL with the following make command.

# make

Once the build process is complete, install postgresql with the following command.

# make install

Postgresql 15 was installed in the /opt/PostgreSQL directory.

Creating a Postgres User

7. Now create a postgres user and a directory that will be used as the data directory to initialize the database cluster. This data directory should be owned by the postgres user, with permissions of 700, and set the path for the postgresql binaries for our convenience.

# useradd postgres
# passwd postgres
# mkdir -p /pgdatabase/data
# chown -R postgres. /pgdatabase/data
# echo 'export PATH=$PATH:/opt/PostgreSQL/bin' > /etc/profile.d/postgres.sh
# source /etc/profile.d/postgres.sh

Initializing a Postgres Database

8. Now initialize the database with the following command as the postgres user before using any postgres commands.

# su postgres
$ initdb -D /pgdatabase/data/ -U postgres -W

Where -D is the location of the database cluster, or let's say it's the data directory where we want to initialize the database cluster, -U is the name of the database superuser, and -W is the password prompt for the database superuser.

For more information and options, we can refer to initdb -help.

9. After initializing the database, starting the database cluster, or if you need to change the server's listening port or address, edit the /pgdatabase/data/postgresql.conf file in the database server's data directory.

$ pg_ctl -D /pgdatabase/data/ start

10. After starting the database, check the status of the postgres server process using the following ps and netstat commands.

$ ps -ef |grep -i postgres
$ netstat -apn |grep -i 51751

We can see that the database cluster is running normally and the startup logs can be found in the location specified with the -l option when starting the database cluster.

11. Now connect to the database cluster and create a database using the following commands.

$ psql -p 51751
postgres=# create database test;
postgres=# l to list all databases in cluster
postgres=# q to quit form postgres console

2786300cookie-checkHow to install PostgreSQL from source on Linuxno

similar

Инструкции,Программы,Рабочее окружение,linux,Postgres,PostgreSQL
#install #PostgreSQL #source #Linux


Back to Featured Articles on Logo Paperblog