8.6. Securing PostgreSQL by limiting access to authenticated local users
Secure your PostgreSQL database by configuring client authentication to limit access only to authenticated local users. This reduces the risks of unauthorized access and attacks.
PostgreSQL is an object-relational database management system (DBMS). In Red Hat Enterprise Linux, PostgreSQL is provided by the postgresql-server package.
The pg_hba.conf configuration file, stored in the database cluster’s data directory, specifies the client authentication settings. The following procedure details how to configure PostgreSQL for host-based authentication.
Procedure
Install PostgreSQL:
# dnf install postgresql-serverInitialize a database storage area using one of the following options:
Using the
initdbutility:$ initdb -D /home/postgresql/db1/The
initdbcommand with the-Doption creates the directory you specify if it does not already exist, for example/home/postgresql/db1/. This directory then contains all the data stored in the database and also the client authentication configuration file.Using the
postgresql-setupscript:$ postgresql-setup --initdbBy default, the script uses the
/var/lib/pgsql/data/directory. This script helps system administrators with basic database cluster administration.
To allow any authenticated local users to access any database with their usernames, modify the following line in the
pg_hba.conffile:local all all trustThis can be problematic when you use layered applications that create database users and no local users. If you do not want to explicitly control all user names on the system, remove the
localline entry from thepg_hba.conffile.Restart the database to apply the changes:
# systemctl restart postgresqlThe previous command updates the database and also verifies the syntax of the configuration file.