This guide will go through how to install PostgreSQL on Rocky Linux. PostgreSQL is a relational database management system (RDBMS) that emphasizes flexibility and SQL compliance. SQL relational querying and JSON non-relational querying are both supported by PostgreSQL. Some of PostgreSQL features: triggers, foreign keys, materialized views, ACID-compliant transactions, and stored procedures.
How to Install PostgreSQL on Rocky Linux
- Update your system packages.
sudo dnf update
- Next, install PostgresSQL using the following command.
sudo dnf install postgresql-server
Sample output
Dependencies resolved. ==================================================================== Package Arch Version Repository Size ==================================================================== Installing: postgresql-server x86_64 13.7-1.el9_0 appstream 5.7 M Installing dependencies: postgresql x86_64 13.7-1.el9_0 appstream 1.5 M postgresql-private-libs x86_64 13.7-1.el9_0 appstream 136 k Transaction Summary ==================================================================== Install 3 Packages Total download size: 7.4 M Installed size: 29 M Is this ok [y/N]: y Downloading Packages: (1/3): postgresql-private-libs-13.7-1.el9_0.x86_64.rpm 24 kB/s | 136 kB 00:05 (2/3): postgresql-13.7-1.el9_0.x86_64.rpm 26 kB/s | 1.5 MB 01:00 (3/3): postgresql-server-13.7-1.el9_0.x86_64.rpm 40 kB/s | 5.7 MB 02:26 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 48 kB/s | 7.4 MB 02:38 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : postgresql-private-libs-13.7-1.el9_0.x86_64 1/3 Installing : postgresql-13.7-1.el9_0.x86_64 2/3 Running scriptlet: postgresql-server-13.7-1.el9_0.x86_64 3/3 Installing : postgresql-server-13.7-1.el9_0.x86_64 3/3 Running scriptlet: postgresql-server-13.7-1.el9_0.x86_64 3/3 Verifying : postgresql-server-13.7-1.el9_0.x86_64 1/3 Verifying : postgresql-private-libs-13.7-1.el9_0.x86_64 2/3 Verifying : postgresql-13.7-1.el9_0.x86_64 3/3 Installed: postgresql-13.7-1.el9_0.x86_64 postgresql-private-libs-13.7-1.el9_0.x86_64 postgresql-server-13.7-1.el9_0.x86_64 Complete!
- After PostgreSQL installation, initialize it using the command below.
sudo postgresql-setup --initdb
- Then start PostgreSQL.
sudo systemctl start postgresql
- Next, enable it to start on system boot.
sudo systemctl enable postgresql
- Confirm PostgreSQL is running.
sudo systemctl status postgresql
Sample output
● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2022-09-10 10:53:16 EAT; 1min 19s ago Main PID: 4903 (postmaster) Tasks: 8 (limit: 23284) Memory: 16.8M CPU: 115ms CGroup: /system.slice/postgresql.service ├─4903 /usr/bin/postmaster -D /var/lib/pgsql/data ├─4904 "postgres: logger " ├─4906 "postgres: checkpointer " ├─4907 "postgres: background writer " ├─4908 "postgres: walwriter " ├─4909 "postgres: autovacuum launcher " ├─4910 "postgres: stats collector " └─4911 "postgres: logical replication launcher " Sep 10 10:53:15 localhost.localdomain systemd[1]: Starting PostgreSQL database server... Sep 10 10:53:16 localhost.localdomain postmaster[4903]: 2022-09-10 10:53:16.008 EAT [4903] LOG: redirecting log output to logging collector process Sep 10 10:53:16 localhost.localdomain postmaster[4903]: 2022-09-10 10:53:16.008 EAT [4903] HINT: Future log output will appear in directory "log". Sep 10 10:53:16 localhost.localdomain systemd[1]: Started PostgreSQL database server.
Connect to PostgreSQL on Rocky Linux
- Connect to PostgreSQL by running the command below.
sudo -u postgres psql
- Next, secure PostgreSQL by setting the password.
\password postgres
Create PostgreSQL database
- Run the following command to create a user with a password.
CREATE USER itnixpro WITH PASSWORD 'YourStrongPassword';
- After user creation, run the command below to create the database.
CREATE DATABASE nixdb OWNER itnixpro;
- List databases on your PostgreSQL.
\l
- To exit from the PostgreSQL shell, use the command below.
\q
Sample output
postgres=# \password postgres Enter new password for user "postgres": Enter it again: postgres=# CREATE USER itnixpro WITH PASSWORD 'YourStrongPassword'; CREATE ROLE postgres=# CREATE DATABASE nixdb OWNER itnixpro; CREATE DATABASE postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- nixdb | itnixpro | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 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 (4 rows) postgres=# \q
- That concludes our article on how to install PostgreSQL on Rocky Linux.
Read more on PostgreSQL Documentation
Other Tutorials
Install PostgreSQL on Ubuntu 22.04