Redhat 7 install postgres database

Get latest postgres database repo for redhat 7

# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Get detail information about this repo.
rpm -qi pgdg-redhat-repo

Enable postgres and install, this article use postgres 12 as an example.

# Install postgres 12
sudo yum install -y postgresql12-server

# Install uuid generator extension (optional)
sudo yum install postgresql12-contrib

Initialize database and enable database service

# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable postgresql-12
sudo systemctl start postgresql-12

# Confirm database is running.
sudo systemctl status postgresql-12

Add firewall rule for postgres database

sudo firewall-cmd --add-service=postgresql --permanent
# Open database port
sudo firewall-cmd --permanent --add-port=5432/tcp

# Reload firewall
sudo firewall-cmd --reload

Setup admin user and password

sudo su - postgres 
# Set up password
psql -c "alter user postgres with password 'Yourpass'"
# List all available databases
\l

Update configuration to allow remote connection

# Edit file /var/lib/pgsql/12/data/postgresql.conf
sudo vi /var/lib/pgsql/12/data/postgresql.conf

# Update listen address based on your needs. '*' for all clients
listen_addresses = '*' 

# Update /var/lib/pgsql/12/data/pg_hba.conf file configuration to allow remote connection
sudo vi /var/lib/pgsql/12/data/pg_hba.conf

# Accept from anywhere
host all all 0.0.0.0/0 md5

# Accept from trusted subnet
host all all 192.168.0.0/24 md5

Restart postgres

sudo systemctl restart postgresql-12

Leave a Comment