How to install Postgresql on CentOS 8

PostgreSQL or Postgres is an open source, multi-purpose relational database management system. It has many advanced features that allow you to build fault-tolerant environments or complex applications.

In this guide, we will discuss how to install the PostgreSQL database server on CentOS 8. Before choosing which version you want to install, make sure your app supports it.

We will explore the basics of PostgreSQL database management.

1. Prerequisites##

In order to install the package, you need to log in to the system as root or another user with sudo privileges.

2. Install Postgresql on CentOS 8

At the time of writing this article, there are two PostgreSQL server versions available for installation: 9.6 and 10.0.

In order to list the available PostgreSQL modules, enter:

dnf module list postgresql

The output shows that there are two versions of postgresql. Each version has two parts: server and client. Server version 10 is the default version:

CentOS-8- AppStream
Name          Stream    Profiles              Summary
postgresql    10[d]    client, server [d]    PostgreSQL server and client module
postgresql    9.6       client, server [d]    PostgreSQL server and client module
  1. To install the default version, PostgreSQL server version 10.0, enter:
sudo dnf install @postgresql:10
  1. To install PostgreSQL server version 9.6, enter:
sudo dnf install @postgresql:9.6

You may also want to install the contrib packages, which provide some additional features to the PostgreSQL database:

sudo dnf install postgresql-contrib

Once the installation is complete, use the following command to initialize the PostgreSQL database:

sudo postgresql-setup initdb
Initializing database ... OK

Start the PostgreSQL service, and enable the PostgreSQL service at boot.

sudo systemctl enable --now postgresql

Use the psql tool to connect to the PostgreSQL database server and print its version number to verify the installation process.

sudo -u postgres psql -c "SELECT version();"
PostgreSQL 10.6 on x86_64-redhat-linux-gnu, compiled by gcc(GCC)8.2.120180905(Red Hat 8.2.1-3),64-bit

Three, PostgreSQL role and identity authentication method##

PostgreSQL supports multiple authentication methods. The most commonly used methods are as follows:

PostgreSQL client authentication is usually defined in the pg_hba.conf file. By default, for local connections, PostgreSQL is set to authenticate against peers.

In order to log in to the PostgreSQL server as the postgres user, first switch the user, and then use the psql tool to access PostgreSQL.

sudo su - postgres
psql

From here, you can interact with the PostgreSQL instance. Exit the PostgreSQL Shell and enter:

\ q

You can also use the sudo command to access PostgreSQL without switching users:

sudo -u postgres psql

Usually, the postgres user is only used locally.

Fourth, create a PostgreSQL role and database##

Only super users and roles with CREATEROLE permission can create new roles.

In the following example, we create a role named john, a database named johndb, and grant permissions on the database

  1. First, connect to the PostgreSQL shell:
sudo -u postgres psql
  1. Create a new PostgreSQL role with the following command:
create role john;
  1. Create a new database:
create database johndb;
  1. Through the following query statement, grant the user all permissions on the database.
grant all privileges on database johndb to john;

Five, enable remote access to PostgreSQL server##

By default, the PostgreSQL server only listens on the local network interface: 127.0.0.1.

To allow remote access to your PostgreSQL server, open the configuration file:

sudo nano /var/lib/pgsql/data/postgresql.conf

Swipe down to the CONNECTIONS AND AUTHENTICATION section, and add or edit the following line:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses ='*'     # what IP address(es) to listen on;

Save the file and restart the PostgreSQL service using the following command:

sudo systemctl restart postgresql

Use the ss tool to verify this modification:

ss -nlt | grep 5432
LISTEN   01280.0.0.0:54320.0.0.0:*
LISTEN   0128[::]:5432[::]:*

The above output shows that the PostgreSQL server is already listening on the default port of all network interfaces (0.0.0.0).

The last step is to configure the server to accept remote connections by editing the pg_hba.conf file.

Here are some examples showing different user examples:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# The user jane can access all databases from all locations using an md5 password
host    all             jane            0.0.0.0/0                md5

# The user jane can access only the janedb database from all locations using an md5 password
host    janedb          jane            0.0.0.0/0                md5

# The user jane can access all databases from a trusted location(192.168.1.134) without a password
host    all             jane            192.168.1.134            trust

Six, summary##

CentOS 8 provides two PostgreSQL versions: 9.6 and 10.0.

For more information on this topic, please visit: PostgreSQL Official Document.

Recommended Posts

How to install Postgresql on CentOS 8
How to install jdk1.8 on centOS7
How to install Memcached on CentOS 8
How to install R on CentOS 8
How to install FFmpeg on CentOS 8
How to install TensorFlow on CentOS 8
How to install Perl 5 on CentOS
How to install Git on CentOS 8
How to install Gradle on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Jenkins on CentOS 8
How to install Java on CentOS 8
How to install PostgreSQL on Ubuntu 16.04
How to install GCC on CentOS 8
How to install Nginx on CentOS 8
How to install Asterisk on CentOS 7
How to install Jenkins on CentOS 8
How to install Vagrant on CentOS 8
How to install Python 3.8 on CentOS 8
How to install Tomcat 9 on CentOS 8
How to install Webmin on CentOS 8
How to install Ruby on CentOS 8
How to install Skype on CentOS 8
How to install htop on CentOS 8
How to install Python on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Wordpress on Centos
How to install htop on CentOS 8
How to install TeamViewer on CentOS 8
How to install MariaDB on CentOS 8
How to install MongoDB on CentOS 7
How to install Odoo 13 on CentOS 8
How to install Apache on CentOS 8
How to install OpenCV on CentOS 8
How to install PHP on CentOS 8
How to install PostgreSQL on Ubuntu 20.04
How to install MongoDB on CentOS 8
How to install Apache Maven on CentOS 8
[Graphic] How to install tomcat on centos
R&D: How To Install Python 3 on CentOS 7
How to install GCC compiler on CentOS 7
How to install offline JDK1.8 on centos7.0
How to install and configure Elasticsearch on CentOS 7
How to install Visual Studio Code on CentOS 8
How to install and use Docker on CentOS 7
How to install RPM packages on CentOS Linux
How to install and configure VNC on CentOS 8
How to install and use Composer on CentOS 8
How to install and configure Redis on CentOS 8
How to install Node.js and npm on CentOS 8
How to install and use PostgreSQL on Ubuntu 16.04
How to install jdk1.8.0_151 and mysql5.6.38 on centos7.2.1511
How to install and configure phpMyAdmin on CentOS 6
How to install and use Curl on CentOS 8
How to install and configure Owncloud on CentOS 8
How to install VirtualBox client extension on CentOS 8
How to install Docker CE on RHEL 8 / CentOS 8
How to install and uninstall tomcat on centos
How to install and configure Redmine on CentOS 8
How to install Ruby on Ubuntu 20.04
How to install Java on Ubuntu 20.04