Step-by-Step Guide
- Update the Package Index
Open a terminal and update the package index to ensure you have the latest information about available packages:
sudo yum update -y
- Add the PostgreSQL Repository
Install the PostgreSQL repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
- Disable the Built-in PostgreSQL Module
Disable the built-in PostgreSQL module to avoid conflicts:
sudo yum -qy module disable postgresql
- Install PostgreSQL
Install PostgreSQL using theyum
package manager. ReplaceXX
with your desired PostgreSQL version (e.g.,13
for PostgreSQL 13):
sudo yum install -y postgresqlXX-server
- Initialize the Database
Initialize the PostgreSQL database:
sudo /usr/pgsql-XX/bin/postgresql-XX-setup initdb
- Start and Enable PostgreSQL
Start the PostgreSQL service:
sudo systemctl start postgresql-XX
Enable PostgreSQL to start on boot:
sudo systemctl enable postgresql-XX
- Verify PostgreSQL Installation
Check the PostgreSQL service status to ensure it is running:
sudo systemctl status postgresql-XX
If PostgreSQL is running, you should see an active (running) status.
- Log In to PostgreSQL
Switch to thepostgres
user and access the PostgreSQL prompt:
sudo su - postgres
psql
- Set Up a New PostgreSQL Role
To create a new PostgreSQL role, use the following SQL command within the PostgreSQL prompt:
CREATE USER your_username WITH PASSWORD 'your_password';
- Create a New Database
To create a new database owned by the new user, use the following SQL command:CREATE DATABASE your_database_name OWNER your_username;
- Grant Privileges (Optional)
To grant all privileges on the database to the new user, use the following SQL command:GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;
- Exit PostgreSQL Prompt
Exit the PostgreSQL prompt by typing:\q
- Test PostgreSQL Connection
To test the connection, switch back to your regular user account and try connecting to the new database:psql -U your_username -d your_database_name
You will be prompted to enter the password for the new user. Once logged in, you can list the tables to ensure the connection is successful:\dt
- Backup and Restore Databases (Optional)
To back up a PostgreSQL database, use thepg_dump
command:bash pg_dump -U your_username your_database_name > your_database_name.sql
To restore a database from a backup file, use thepsql
command:bash psql -U your_username -d your_database_name < your_database_name.sql