PostgreSQL is a powerful and reliable open-source object-relational database system. With over 30 years of active development, it has earned a strong reputation for its robust features and excellent performance. In this guide, we will walk you through the step-by-step process of installing PostgreSQL in Rocky Linux 8.
1. Listing the PostgreSQL Module
Before we begin the installation process, let’s list the available PostgreSQL modules in Rocky Linux 8. Open your terminal and enter the following command:
dnf module list postgresql
The output will display the available PostgreSQL modules along with their streams and profiles:
Name |
Stream |
Profiles |
Summary |
---|---|---|---|
postgresql |
9.6 |
client, server [d] |
PostgreSQL server and client module |
postgresql |
10 |
client, server [d] |
PostgreSQL server and client module |
postgresql |
12 |
client, server [d] |
PostgreSQL server and client module |
Note: [d] indicates the default version.
2. Enabling the PostgreSQL Module
To enable the latest version of PostgreSQL module, which is version 12, use the following command:
dnf module enable postgresql:12
Confirm the selection by typing ‘y’ when prompted.
3. Installing the PostgreSQL Server
After enabling the PostgreSQL module, you can proceed with installing the postgresql-server package. Execute the following command:
dnf install postgresql-server
This command will install the necessary dependencies along with the PostgreSQL server package.
4. Initializing the PostgreSQL Database Cluster
Before starting the PostgreSQL service, you need to initialize the database storage area on disk. Use the following command to perform the initialization:
postgresql-setup --initdb
This command will initialize the database in the ‘/var/lib/pgsql/data’ directory. You can find the logs in ‘/var/lib/pgsql/initdb_postgresql.log’.
5. Starting and Enabling the PostgreSQL Service
To start the PostgreSQL service, enter the following command:
systemctl start postgresql
To ensure that the service starts automatically at boot time, enable it using the command:
systemctl enable postgresql
Verify the status of the PostgreSQL service by running:
systemctl status postgresql
If the service is active and running, you will see the ‘active (running)’ status in the output.
6. Managing PostgreSQL Roles
To manage PostgreSQL roles, we need to switch to the ‘postgres’ account. Execute the following command:
sudo -i -u postgres
Once you are logged in as the ‘postgres’ user, you can access the PostgreSQL prompt using the ‘psql’ utility:
psql
You will see the PostgreSQL prompt, indicating that you are connected to the database.
7. Creating a PostgreSQL Role
To create a new PostgreSQL role, use the ‘createuser’ command followed by the ‘–interactive’ flag:
createuser --interactive
Enter the name of the role you want to add when prompted. For example, let’s create a role named ‘jones’. You can also choose to make the new role a superuser.
8. Creating a PostgreSQL Database
After creating the role, you can proceed with creating a PostgreSQL database. Use the ‘createdb’ command followed by the desired database name. For example:
createdb db_name
Replace ‘db_name’ with the name you want to give to your database. For instance, if you want to create a database named ‘jones’, use the following command:
createdb jones
9. Opening a PostgreSQL Prompt with the New Role
To connect to the PostgreSQL database with the newly created role, you need to add the user to your system. Execute the following command:
sudo adduser jones
Replace ‘jones’ with the username you created earlier.
Switch to the newly created user account using the following command:
sudo -i -u jones
Now, you can access the PostgreSQL prompt by running the ‘psql’ command:
psql
You will be logged into the PostgreSQL prompt as the ‘jones’ user.
10. Additional Information: Shape.host Services
At Shape.host, we offer reliable and scalable Linux SSD VPS hosting solutions. Our services provide efficient and secure environments for running PostgreSQL and other applications. With our high-performance infrastructure, you can ensure optimal performance for your database needs.
Conclusion
Installing PostgreSQL in Rocky Linux 8 is a straightforward process. By following the steps outlined in this guide, you can set up your PostgreSQL server and manage roles and databases effectively. Whether you are a beginner or an experienced user, PostgreSQL offers a feature-rich and reliable database solution for your applications.
Shape.host provides Linux SSD VPS hosting services, ensuring the stability and performance of your PostgreSQL databases. Trust us to deliver secure and scalable hosting solutions tailored to your business needs.