What is PostgreSQL?

PostgreSQL is a powerful, open source object-relational database system ( ORDBMS ). Unlike other relational database systems, PostgreSQL (Postgres) allows users to create unique operators, complex data types, aggregate functions, data type conversion character, and other various database objects through the SQL function.

In this article, we are going to show you how to install PostgreSQL 11 (Postgres) database on CentOS 7 or RHEL 7.

Step 1: Add PostgreSQL YUM Repository

– Add PostgreSQL yum repository

# yum install -y https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

Step 2: Install PostgreSQL

# sudo yum install postgresql11-server

– Now that the database packages have been installed, Initialize the database by running the following command:

# sudo /usr/pgsql-11/bin/postgresql-11-setup initdb Initializing database ... OK

– Use the below commands to start, enable the PostgreSQL 11 and check its status

# sudo systemctl start postgresql-11.service # sudo systemctl enable postgresql-11.service # sudo systemctl status postgresql-11.service

Step 3: Connect to PostgreSQL

– After installing PostgreSQL 11 database server, by default PostgreSQL, will create a system account user named postgres with role postgres.

– To connect to the PostgreSQL database server, use the following commands:

~# su -l postgres ~$ psql psql (11.1 )) Type "help" for help. postgres=#

Step 4: Usage Examples

– To check log info use the below comand:

postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".

– To change the password of postgres role you can use:

postgres=# \password postgres

– To list all the databaes on your PostgreSQL database server just run one of the following commands:

postgres=# \list postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)

– To connect to a specific database on your PostgreSQL database server you can the below command \c followed by the name of your database:

postgres=# \c postgres

You are now connected to database “postgres” as user “postgres”.

– List all the tables after have been connected to a specific database

postgres=# \d

– So, to create a new database on your PostgreSQL database server you can use one the following commands followed by the database name :

postgres=# createdb database_name postgres=# createdb database_name OWNER rolename ;

– let’s create a table by using the following command:

postgres=# create table employees (name varchar(25), surname varchar(25)) ;

– To insert records into a specific tables, use the INSERT query as below:

postgres=# INSERT INTO employees VALUES ('Lotfi','waderni') ;

– Finally to reave the psql command line, execute the below command:

postgres=# \q

Conclusion

You have successfully installed the PostgreSQL 11 database server on CentOS/RHEL 7 system. Here are some more guides that cover how to use PostgreSQL (Postgres):

We hope this tutorial was enough Helpful. If you need more information, or have any questions, just comment below and we will be glad to assist you!