How to install PostgreSQL on CentOS using Yum

postgresql active queries

PostgreSQL is a powerful, open-source relational database system that’s widely used for its reliability and flexibility. If you’re setting up PostgreSQL on CentOS, you’re in the right place. This guide will walk you through the installation process, from adding the necessary repository to configuring the database for remote access.

Step 1: Set Up the PostgreSQL Repository

To begin, you need to add the PostgreSQL Yum repository. This ensures you’re installing the latest and most stable version compatible with your CentOS system.

Run the following command:

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-centos13-13-2.noarch.rpm

This command installs the repository for PostgreSQL version 13. If you require a different version, visit the PostgreSQL download page to locate the appropriate repository package.

Step 2: Install PostgreSQL

Once the repository is added, install the PostgreSQL server and client:

sudo yum install -y postgresql13-server postgresql13

This command will fetch and install all the required packages. Replace 13 with the desired version number if you’re working with a different version.

Step 3: Initialize the Database

After installation, you need to initialize PostgreSQL. This process sets up the required data directories for the database.

sudo /usr/pgsql-13/bin/postgresql-13-setup initdb

This step is mandatory before starting the PostgreSQL service.

See also  A Deep Dive into Pgpool-II for PostgreSQL Load Balancing

Step 4: Start and Enable PostgreSQL

Now, you’re ready to start the PostgreSQL service. Enable it to ensure it starts automatically when the server boots:

sudo systemctl start postgresql-13
sudo systemctl enable postgresql-13

You can check if PostgreSQL is running correctly by using:

sudo systemctl status postgresql-13

Step 5: Optional Configuration for Remote Access

If you plan to access your database from another system, you’ll need to make a few configuration changes:

Modify postgresql.conf

Edit the file to allow the server to listen on all IP addresses:

sudo vi /var/lib/pgsql/13/data/postgresql.conf

Locate the line starting with listen_addresses and update it:

listen_addresses = '*'

Update pg_hba.conf

Open the file to configure client authentication:

sudo vi /var/lib/pgsql/13/data/pg_hba.conf

Add the following entry to allow connections from all IPs:

host    all             all             0.0.0.0/0               md5

Finally, restart the PostgreSQL service to apply these changes:

sudo systemctl restart postgresql-13

Step 6: Secure Your Installation

For added security, set a password for the default PostgreSQL postgres user. Enter the PostgreSQL shell:

sudo -u postgres psql

Within the shell, run:

\password postgres

Enter and confirm the new password.

Step 7: Verify the Installation

Confirm that PostgreSQL is installed and running properly by checking the version:

psql --version

This command will display the installed PostgreSQL version, verifying that everything is set up correctly.

Troubleshooting Tips

  • Error: Repository Not Found: Double-check the repository URL for typos or version mismatches.
  • Service Not Starting: Ensure your firewall rules allow PostgreSQL to communicate. Use sudo firewall-cmd --add-service=postgresql --permanent followed by sudo firewall-cmd --reload.

FAQs

How do I uninstall PostgreSQL?

Run sudo yum remove postgresql13* to remove PostgreSQL and its related packages. Be cautious, as this will delete all associated data.

Where can I find the logs?

PostgreSQL logs are typically located in /var/lib/pgsql/13/data/pg_log/. You can use tail -f to monitor logs in real time.

With PostgreSQL installed and configured, your database is ready to handle data-intensive applications. Following these steps will give you a secure and functional database environment on CentOS.

3 thoughts on “How to install PostgreSQL on CentOS using Yum”

  1. When service postgresql initdb:

    If (“Data directory is not empty”)
    {
    (
    rm -f -r /usr/local/pgsql/data
    or
    rm -f -r /var/lib/pgsql/data
    )
    su postgres
    initdb -D /var/lib/pgsql/data
    };

Leave a Comment