Initial setup

Install packages, run initial database setup and start the service

# dnf install postgresql postgresql-contrib postgresql-server
# postgresql-setup --initdb
# systemctl enable --now postgresql

Setup a password for default postgres user

# su - postgres
$ psql
\password postgres
<enter_password>
\quit

Access

Create a user and database - execute commands as postgres (operating system) user. Don’t assign a password to the user, i.e., don’t use -P option, if you are going to use peer authentication method and unix domain socket to access the database. Read the pg_hba.conf documentation page for details on the client authentication.

$ createuser testuser -P
$ createdb --owner=testuser testdatabase

Edit /var/lib/pgsql/data/pg_hba.conf as postgres user or root and define authentication method for the host, database and user that suits your needs.

Example. Configuration allowing ipv4 connections to localhost using password verification for testuser and testdatabase.

[user]$ cat /var/lib/pgsql/data/pg_hba.conf
# TYPE  DATABASE        USER        ADDRESS         METHOD
host    testdatabase    testuser    127.0.0.1/32    scram-sha-256

Restart the postgresql service to apply changes and try to establish a connection

# systemctl restart postgresql
$ psql -h 127.0.0.1 -U testuser testdatabase

Firewall

If you are going to access the database outside the host machine you might need to open ports. Check port number in /var/lib/pgsql/data/postgresql.conf.

PostgreSQL operates on default port 5432

# firewall-cmd --add-service=postgresql --permanent
# firewall-cmd --reload

PostgreSQL operates on non-standard port

# firewall-cmd --add-port=PORT_NUMBER/tcp --permanent
# firewall-cmd --reload

Do not open firewall ports if you don’t understand why you need that and what risks involved.

Reference