PostgreSQL is a powerful, open-source, object-relational database system that has earned a strong reputation for its performance, reliability, and data integrity. In this tutorial, we will walk you through the process of how to install PostgreSQL on Scientific Linux step by step.
Table of Contents
- Prerequisites
- Install the PostgreSQL repository
- Install PostgreSQL
- Initialize the PostgreSQL database
- Start and enable PostgreSQL service
- Secure the PostgreSQL installation
- Create a new PostgreSQL database and user
- Test the PostgreSQL installation
- Conclusion
How to Install PostgreSQL on Scientific Linux
1. Prerequisites
Before we begin, make sure that your Scientific Linux system is up to date:
sudo yum update -y
Additionally, you should have root access or a user account with sudo
privileges.
2. Install the PostgreSQL repository on Scientific Linux
To install the PostgreSQL repository, run the following command:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
This command downloads and installs the latest PostgreSQL repository for Scientific Linux.
3. Install PostgreSQL on Scientific Linux
Now that the repository is in place, you can install PostgreSQL by executing the following command:
sudo yum install -y postgresql13 postgresql13-server
This command installs PostgreSQL 13 and its server package on your system.
4. Initialize the PostgreSQL database on Scientific Linux
Once the installation is complete, you need to initialize the PostgreSQL database using the postgresql-setup
command:
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
This command initializes the PostgreSQL database and creates the necessary data directories.
5. Start and enable PostgreSQL service on Scientific Linux
To start the PostgreSQL service, run the following command:
sudo systemctl start postgresql-13
To enable the PostgreSQL service to start automatically on boot, execute:
sudo systemctl enable postgresql-13
6. Secure the PostgreSQL installation on Scientific Linux
By default, PostgreSQL is configured to use “ident” authentication. To change this to a more secure method, like “md5”, you need to edit the pg_hba.conf
file:
sudo nano /var/lib/pgsql/13/data/pg_hba.conf
Find the following lines:
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
Replace ident
with md5
for both IPv4 and IPv6 local connections:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
Save and close the file. To apply the changes, restart the PostgreSQL service:
sudo systemctl restart postgresql-13
7: Create a PostgreSQL User and Database on Scientific Linux
Now that your PostgreSQL installation is up and running, let’s create a new user and database. First, switch to the postgres user:
sudo su - postgres
Next, create a new PostgreSQL user by running the createuser
command. Replace {username}
with your desired username:
createuser {username}
Now, create a new database and assign the newly created user as its owner. Replace {dbname}
with your desired database name:
createdb {dbname} --owner={username}
After creating the new user and database, exit the postgres user session:
exit
8: Configure Remote Access on Scientific Linux (Optional)
By default, PostgreSQL is configured to only accept connections from the localhost. If you need to access your PostgreSQL server remotely, you’ll need to modify the pg_hba.conf
and postgresql.conf
files.
First, edit the pg_hba.conf
file:
sudo nano /var/lib/pgsql/13/data/pg_hba.conf
Add the following line at the end of the file, replacing {your_network}
with your network’s IP address range, and {your_netmask}
with your network’s netmask:
host all all {your_network}/{your_netmask} md5
Save and close the file.
Next, edit the postgresql.conf
file:
sudo nano /var/lib/pgsql/13/data/postgresql.conf
Find the line that starts with #listen_addresses
and uncomment it by removing the #
symbol. Change the value to '*'
to allow connections from any IP address:
listen_addresses = '*'
Save and close the file. To apply the changes, restart the PostgreSQL service:
sudo systemctl restart postgresql-13
Conclusion
You’ve successfully installed PostgreSQL on your Scientific Linux system. Now you can start using this powerful open-source relational database management system for your projects. For further information, check out the official PostgreSQL documentation.
For more guides on setting up various services on Scientific Linux, please visit our other tutorials: