Postgres - Useful Commands

Create a file to store login and password 

 cd /root

vi .pgpass
chmod 600 .pgpass 

 

 

 

 localhost:5432:*:postgres:YOUR_PASSWORD localhost:5433:*:postgres:YOUR_PASSWORD 

 

 

 

 #Use Postgres

sudo -u postgres psql -p 5432

#Tests the conntection

sudo psql -h localhost -p 5432 -U postgres -c '\conninfo'

#Resets password

sudo -u postgres /usr/pgsql-16/bin/psql -p 5432 -c "ALTER ROLE postgres PASSWORD 'YOUR_PASSWORD'"

sudo -u postgres /usr/pgsql-17/bin/psql -p 5433 -c "ALTER ROLE postgres PASSWORD 'YOUR_PASSWORD'" 

 Last verified: October 2026 Sources: https://www.postgresql.org/docs/current/libpq-pgpass.html https://www.postgresql.org/docs/current/app-psql.html