Mastering PostgreSQL Setup on CentOS

Setting up PostgreSQL on CentOS can seem daunting at first, but don’t worry—I’m here to help guide you through each step. Whether you’re a beginner dipping your toes into the world of databases or a seasoned pro looking to refresh your knowledge, understanding how to properly set up PostgreSQL on CentOS is crucial. In this comprehensive guide, we’ll walk through everything from installing PostgreSQL using YUM to configuring your database for optimal performance. Let’s dive right in!

Installing PostgreSQL on CentOS

Getting PostgreSQL up and running is the first step. Here, I’ll explain how to set up your database server on CentOS using different methods, including using the YUM package manager.

PSQL Install on CentOS

First things first, you need to ensure your CentOS system is up to date. Here’s how you can do that:

Once your system is updated, you can proceed with installing PostgreSQL. The easiest way to install PostgreSQL on CentOS is by utilizing the YUM package manager. This package manager simplifies the process by handling all necessary dependencies for you.

Now, let’s install PostgreSQL:

This command will install the PostgreSQL server and its additional features or modules. After installation, you’ll need to initialize the database:

And now, you can start the PostgreSQL service:

Make sure PostgreSQL starts on boot:

Here’s a personal tip: Always verify that PostgreSQL is running smoothly. You can do this by checking the service status:

Postgres Configuration on CentOS

Configuring PostgreSQL properly is vital to ensure your database serves your needs effectively. The default configuration is usually suitable for development environments, but changes are often necessary for production environments.

Edit the PostgreSQL configuration file, typically found at /var/lib/pgsql/data/postgresql.conf.

In this file, you can adjust settings like max_connections to control how many concurrent connections your database can handle.

For network access, edit the pg_hba.conf file, located in the same directory:

Add the following line for IPv4 access (or modify an existing appropriate line):

Don’t forget to restart the PostgreSQL service once you’ve made these changes:

Great, now your PostgreSQL server should be more tailored to your needs!

Yum Install PostgreSQL-14

At times, you might need a specific version of PostgreSQL for compatibility or feature reasons. Let’s focus on installing PostgreSQL-14 on CentOS using YUM.

First, you need to add the PostgreSQL YUM repository:

This command adds the PostgreSQL 14 repository, enabling you to install this version specifically.

Now, install PostgreSQL-14:

Initialize the database as before, but note the version number in the path:

Start and enable the PostgreSQL-14 service:

With these steps, PostgreSQL-14 will be ready to use!

How to Configure PostgreSQL in CentOS?

PostgreSQL’s configuration can significantly impact its performance and security. Let’s break down the essential aspects of configuring PostgreSQL on CentOS.

Firewall Settings and Network Accessibility

To allow external access to your PostgreSQL server, you’ll need to adjust your firewall settings. Here’s how you can permit PostgreSQL access through the firewall:

Make sure your postgresql.conf file has the following line to allow connections from any IP address:

Optimizing Performance

There are numerous settings to fine-tune PostgreSQL’s performance based on your server’s specifications and the nature of your applications. Here’s my advice for a few common adjustments:

  • Memory Allocation: Set shared_buffers to approximately 25% of your server’s RAM.
  • Work Memory: Adjust work_mem for each connection that performs large sorts or join operations.

After making these changes, always restart PostgreSQL:

Security Configurations

An often overlooked but crucial part of any PostgreSQL setup is ensuring security of your data. Here are some common practices:

  • User and Role Management: Create specific roles with tailored permissions instead of using the default postgres user.
  • Password Policies: Enforce strong passwords by setting password_encryption to scram-sha-256 in postgresql.conf.

These configurations can make your database more secure and performant. Let me share a small personal experience—I lost a significant amount of time due to performance issues, which could have been avoided with proper initial configuration. So, don’t skip this step!

Installing PostgreSQL on RHEL 9

RHEL (Red Hat Enterprise Linux), like CentOS, offers strong support for PostgreSQL, but the process is slightly different. Let’s go through it.

Initial Setup for RHEL 9

Start by updating your system:

Add the PostgreSQL repository specific to RHEL 9:

Installing PostgreSQL

Now, install PostgreSQL using DNF (the YUM replacement in RHEL/CentOS 8+):

Initialize the PostgreSQL database system:

Enabling Remote Access

For applications outside your local network to communicate with your PostgreSQL database, you’ll need to tweak a few settings:

  • Edit postgresql.conf to listen on all available interfaces:

  • Configure pg_hba.conf for password authentication:

Restart PostgreSQL to apply these changes:

With these steps, your PostgreSQL on RHEL 9 should be functioning well!

Installing Postgres 10 on CentOS 7

For those specifically interested in a stable and earlier version of PostgreSQL, I’ll detail how to set up PostgreSQL 10 on CentOS 7.

Preparing for Installation

Begin with adding PostgreSQL 10 repository:

Stop any conflicting PostgreSQL modules available in the default CentOS repositories:

Proceeding with Installation

Install PostgreSQL 10 server and additional components:

Initialize the database:

Start and enable PostgreSQL 10:

Configuring Firewalls and Connections

Update your firewall rules to allow PostgreSQL traffic:

Make essential changes to postgresql.conf and pg_hba.conf for remote connectivity, as outlined earlier.

Restarting your PostgreSQL 10 service will apply all your configurations:

With PostgreSQL 10 running, you’re free to harness its capabilities for your applications on CentOS 7.

FAQs About PostgreSQL Setup on CentOS

What is the default PostgreSQL port?

PostgreSQL by default listens on port 5432.

Can I install multiple PostgreSQL versions on one server?

Yes, by using different binaries and data directories, multiple versions can coexist.

How do I stop a PostgreSQL service?

You can stop it using:

What should I do if I forget the PostgreSQL password?

You can reset it using the psql command line by logging in with a superuser role or through altering the user password within SQL commands.


I remember when I first set up PostgreSQL—I was both excited and apprehensive. But with practice and patience, it turned into a seamless experience. I hope this guide makes your journey just as smooth and rewarding. Drop any questions you have in the comments, and I’ll be happy to help out. Happy databasing!

You May Also Like