Installation guides

Installing PostgreSQL on RHEL

Step-by-Step Guide

  1. 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
  1. 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
  1. Disable the Built-in PostgreSQL Module
    Disable the built-in PostgreSQL module to avoid conflicts:
   sudo yum -qy module disable postgresql
  1. Install PostgreSQL
    Install PostgreSQL using the yum package manager. Replace XX with your desired PostgreSQL version (e.g., 13 for PostgreSQL 13):
   sudo yum install -y postgresqlXX-server
  1. Initialize the Database
    Initialize the PostgreSQL database:
   sudo /usr/pgsql-XX/bin/postgresql-XX-setup initdb
  1. Start and Enable PostgreSQL
    Start the PostgreSQL service:
   sudo systemctl start postgresql-XX

Enable PostgreSQL to start on boot:

   sudo systemctl enable postgresql-XX
  1. 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.

  1. Log In to PostgreSQL
    Switch to the postgres user and access the PostgreSQL prompt:
   sudo su - postgres
   psql
  1. 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';
  1. 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;
  2. 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;
  3. Exit PostgreSQL Prompt
    Exit the PostgreSQL prompt by typing: \q
  4. 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
  5. Backup and Restore Databases (Optional)
    To back up a PostgreSQL database, use the pg_dump command:
    bash pg_dump -U your_username your_database_name > your_database_name.sql
    To restore a database from a backup file, use the psql command:
    bash psql -U your_username -d your_database_name < your_database_name.sql
Other Recent Posts