PostgreSQL database backup with pg_dump by using .pgpass file

 It is better not to use password in commands so for taking backups from postgresql database we can store credentials in special file and give it to command so with this way it can be possible to run commands securely and password will be read from .pgpass file automatically

/usr/bin/pg_dump -F c -h localhost -U yourdbuser yourdbname > /path_$(date '+%d%m%Y_%H%M%S').sql.zip



cat /home/yourOSuser/.pgpass 

localhost:5432:yourdbname:yourdbuser:yourdbuserpassword


if order to make it work also do not forget to change connection method


/var/lib/pgsql/14/data/pg_hba.conf file


# "local" is for Unix domain socket connections only

local   all         all    peer  ->>>> change this to md5 to login with user


and restart your postgres database to reflect changes


sudo service postgresql-14.service status




how about restore

PS: this is not regular zip file do not try to unzip this!



create database first


sudo -u postgres psql


postgres=# create database yourdbname;


postgres=# GRANT ALL PRIVILEGES ON DATABASE yourdbname to yourdbuser;


pg_restore -h localhost -U yourdbuser -C -d yourdbname /path_$(date '+%d%m%Y_%H%M%S').sql.zip

Comments

Popular posts from this blog

Pyppeteer fix for BrowserError: Browser closed unexpectedly

overlay filesystem and containers

How to add pagination to django comments for your model