PostgreSQL is a powerful, open source object-relational database system ( ORDBMS ). Unlike other relational database systems, PostgreSQL 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 12 database on CentOS 8.
Step 1: Installing PostgreSQL
PostgreSQL is available from CentOS 8’s default AppStream software repository, and there are multiple versions which you can install.
# dnf module list postgresql postgresql 9.6 client, server [d] PostgreSQL server and client module postgresql 10 [d] client, server [d] PostgreSQL server and client module postgresql 12 client, server PostgreSQL server and client module
You can see in this output that there are three versions of PostgreSQL available from the AppStream repository: 9.6, 10, and 12.
The default stream is the one that provides Postgres version 10.
01- If you want to install the version 10 you could just run
sudo dnf install postgresql-server
02- To install PostgreSQL version 12. First, you have to enable the module stream of PostgreSQL 12
$ sudo dnf module enable postgresql:12
After enabling the version 12 module stream, let’s install PostgreSQL 12 and all of its dependencies by running the below command:
$ sudo dnf install postgresql-server
– Once the installation is complete, initialize the PostgreSQL database with the following command:
$ sudo postgresql-setup initdb Initializing database ... OK
– Start/Enable PostgreSQL:
$ sudo systemctl enable --now postgresql && sudo systemctl start postgresql
– Use the psql
tool to verify the installation by connecting to the PostgreSQL database server and print its version:
$ sudo -u postgres psql -c "SELECT version();"
Step 2: Accessing Database
– Switch into the postgres user:
$ su – postgres
– Connect to the PostgreSQL terminal:
$ psql
– Exit from PosgreSQL prompt:
# \q
Step 3: Usage Examples
– List all the databases:
# \list
– Connect to a database:
# \c database_name
– List all the tables
# \d
– Create a Database
# createdb database_name # createdb database_name OWNER rolename;
– Create a table
# create table employees (name varchar(25), surname varchar(25));
– Insert records
# INSERT INTO employees VALUES ('Lotfi','waderni');
Conclusion
You are learnt how to set up PostgreSQL on your CentOS 8 server. However, there is still much more to learn with Postgres. For more information visit the PostgreSQL Documentation
Here are some more guides that may interest you: