Install PostgreSQL on CentOS 7

Updated on August 11, 2021
Install PostgreSQL on CentOS 7 header image

Introduction

PostgreSQL is one of the most advanced open source Relational Database Management Systems (RDBMS). It is ANSI SQL:2008 standards compliant and has gained more buzz lately due to its addition of JSON and JSONB native data types, causing it to be looked at as a viable solution to problems NoSQL databases, such as MongoDB, are traditionally used to solve.

Follow this guide to install PostgreSql on CentOS 7.

Prerequisites

Installation

  1. Add the PostgreSQL repository.

     $ sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  2. Install PostgreSQL.

     $ sudo yum install -y postgresql13-server
  3. Initialize the database.

     $ sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
  4. Enable and start the service.

     $ sudo systemctl enable --now postgresql-13

Usage

PostgreSQL creates a default user to access the psql shell.

  1. Switch to the user and execute a shell.

     $ sudo -iu postgres psql
  2. Create a database named test.

     $ CREATE DATABASE test;
  3. Connect to the new database named test.

     $ \c test
  4. Create a table named messages.

     $ CREATE TABLE messages (handle VARCHAR(32), message VARCHAR(280));
  5. Insert a couple rows into the new table named messages.

     $ INSERT INTO messages VALUES ('User', 'This is a test message.');
     $ INSERT INTO messages VALUES ('User', 'This is another test message.');
  6. Query all rows from the table named messages.

     $ SELECT * FROM messages;

The rows inserted in the earlier steps should be listed.

Conclusion

PostgreSQL has many features that are available for use. Some of them include multi-version concurrency control (MVCC), point in time recovery, tablespaces, asynchronous replication, nested transactions (savepoints), online/hot backups, query planner/optimizer, and write ahead logging for fault tolerance. To get the most out of PostgreSQL, refer to the documentation.