In this short tutorial I’ll guide you through the installation of PostgreSQL 12 on Amazon Linux 2. PostgreSQL is a powerful, free and open-source relational database management system with SQL compliance. The original name was POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley. As of this article writing the latest available release of PostgreSQL database server is version 12.

The PostgreSQL database server have been in active development for over 30 years that has earned it a strong reputation for reliability, feature robustness, and performance. The official documentation has vast amount of information on the architecture, features, installation and day 2 related usage of PostgreSQL database server.

Install PostgreSQL 12 on Amazon Linux 2

This installation will be performed on Amazon 2 Linux server. It doesn’t need to be running in an AWS cloud but can also be in a Virtualized environment within your Infrastructure or Laptop with use of VirtualBox, KVM, VMware or with the help of tools like Vagrant. Before you begin installation confirm you have met the following minimum requirements.

  • Amazon Linux 2 Virtual Machine
  • 2GB of RAM
  • 1 virtual cpu core
  • 1GB of disk space for installation
  • User account with sudo or root user credentials

Once the system is ready, update installed packages to the latest available releases.

sudo yum -y update

Step 1: Add PostgreSQL Yum Repository to Amazon Linux 2

Next we need to add the PostgreSQL repository to our Amazon Linux 2 machine for us to be able to install packages. Also note that PostgreSQL 11 and PostgreSQL 10 packages are available in Amazon Linux extras repository.

$ sudo  amazon-linux-extras | grep postgre
  5  postgresql9.6            available    \
  6  postgresql10             available    [ =10  =stable ]
 41  postgresql11             available    [ =11  =stable ]

To add official PostgreSQL repository to Amazon Linux 2 server, run the following commands as root or user account with sudo privileges.

sudo tee /etc/yum.repos.d/pgdg.repo<<EOF
[pgdg12]
name=PostgreSQL 12 for RHEL/CentOS 7 - x86_64
baseurl=https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64
enabled=1
gpgcheck=0
EOF

Update your packages index file.

sudo yum makecache

Step 2: Install PostgreSQL 12 on Amazon Linux 2

Once the repository has been added we can install PostgreSQL server and client packages on Amazon Linux 2 by using the following commands.

sudo yum install postgresql12 postgresql12-server

Review the list of dependencies to be installed and agree to proceed with the installation.

Resolving Dependencies
--> Running transaction check
---> Package postgresql12.x86_64 0:12.3-5PGDG.rhel7 will be installed
--> Processing Dependency: postgresql12-libs(x86-64) = 12.3-5PGDG.rhel7 for package: postgresql12-12.3-5PGDG.rhel7.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql12-12.3-5PGDG.rhel7.x86_64
---> Package postgresql12-server.x86_64 0:12.3-5PGDG.rhel7 will be installed
--> Running transaction check
---> Package postgresql12-libs.x86_64 0:12.3-5PGDG.rhel7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================================================
 Package                                       Arch                             Version                                    Repository                        Size
==================================================================================================================================================================
Installing:
 postgresql12                                  x86_64                           12.3-5PGDG.rhel7                           pgdg12                           1.6 M
 postgresql12-server                           x86_64                           12.3-5PGDG.rhel7                           pgdg12                           5.0 M
Installing for dependencies:
 postgresql12-libs                             x86_64                           12.3-5PGDG.rhel7                           pgdg12                           369 k

Transaction Summary
==================================================================================================================================================================
Install  2 Packages (+1 Dependent package)

Total download size: 6.9 M
Installed size: 29 M
Is this ok [y/d/N]: y

For any successful installation on Amazon Linux 2 a Complete message should be printed.

Running transaction
  Installing : postgresql12-libs-12.3-5PGDG.rhel7.x86_64                                                                                                      1/3
  Installing : postgresql12-12.3-5PGDG.rhel7.x86_64                                                                                                           2/3
  Installing : postgresql12-server-12.3-5PGDG.rhel7.x86_64                                                                                                    3/3
  Verifying  : postgresql12-libs-12.3-5PGDG.rhel7.x86_64                                                                                                      1/3
  Verifying  : postgresql12-12.3-5PGDG.rhel7.x86_64                                                                                                           2/3
  Verifying  : postgresql12-server-12.3-5PGDG.rhel7.x86_64                                                                                                    3/3

Installed:
  postgresql12.x86_64 0:12.3-5PGDG.rhel7                                       postgresql12-server.x86_64 0:12.3-5PGDG.rhel7

Dependency Installed:
  postgresql12-libs.x86_64 0:12.3-5PGDG.rhel7

Complete!

Step 3: Initialize and start database service

We need to initialize the database server for configuration files to be generated. This is done by calling the setup script.

$ sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
Initializing database ... OK

PostgreSQL 12 server uses configuration file in /var/lib/pgsql/12/data/postgresql.conf . You can review all default values and tune to your liking before using the database server for your Production workloads.

To start and enable the service to start at OS boot, run the following command:

sudo systemctl enable --now postgresql-12

This is the expected execution output:

Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-12.service to /usr/lib/systemd/system/postgresql-12.service.

Check service status to confirm it is in running state.

$ systemctl status postgresql-12
● postgresql-12.service - PostgreSQL 12 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-08-04 15:09:14 UTC; 1min 13s ago
     Docs: https://www.postgresql.org/docs/12/static/
  Process: 21830 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 21835 (postmaster)
   CGroup: /system.slice/postgresql-12.service
           ├─21835 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
           ├─21838 postgres: logger
           ├─21840 postgres: checkpointer
           ├─21841 postgres: background writer
           ├─21842 postgres: walwriter
           ├─21843 postgres: autovacuum launcher
           ├─21844 postgres: stats collector
           └─21845 postgres: logical replication launcher

Aug 04 15:09:14 amazon-linux systemd[1]: Starting PostgreSQL 12 database server...
Aug 04 15:09:14 amazon-linux postmaster[21835]: 2020-08-04 15:09:14.408 UTC [21835] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by...), 64-bit
Aug 04 15:09:14 amazon-linux postmaster[21835]: 2020-08-04 15:09:14.409 UTC [21835] LOG:  listening on IPv4 address "127.0.0.1", port 5432
Aug 04 15:09:14 amazon-linux postmaster[21835]: 2020-08-04 15:09:14.410 UTC [21835] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Aug 04 15:09:14 amazon-linux postmaster[21835]: 2020-08-04 15:09:14.411 UTC [21835] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
Aug 04 15:09:14 amazon-linux postmaster[21835]: 2020-08-04 15:09:14.418 UTC [21835] LOG:  redirecting log output to logging collector process
Aug 04 15:09:14 amazon-linux postmaster[21835]: 2020-08-04 15:09:14.418 UTC [21835] HINT:  Future log output will appear in directory "log".
Aug 04 15:09:14 amazon-linux systemd[1]: Started PostgreSQL 12 database server.
Hint: Some lines were ellipsized, use -l to show in full.

Step 4: Set PostgreSQL admin user’s password

Set PostgreSQL admin user password that you’ll use to escalate privilege for DB operations.

$ sudo su - postgres 
-bash-4.2$ psql -c "alter user postgres with password 'StrongPassword'"
ALTER ROLE

If you need your Application servers to connect to the database server over the network you’ll have to enable remote connections. A better coverage on setting this feature is available in the documentation pages.

More Learning courses:

$15.14
$151.37
in stock
Udemy.com
$15.14
$139.73
in stock
Udemy.com

LEAVE A REPLY

Please enter your comment!
Please enter your name here

3 + 15 =