Postgres Backup and Restore

For backing up a particular database in tar format

pg_dump -U postgres -h 127.0.0.1 -Ft dbname > dbname.tar

If you don't want to type password or use it inside script

PGPASSWORD=pwd pg_dump -U postgres -h 127.0.0.1 -Ft dbname > dbname.tar

Before you restore, connect to postgres database and create an empty database

psql -U postgres -h 127.0.0.1
create database newdatabase;

Then use pg_restore to restore from dumped tar.

PGPASSWORD=pwd pg_restore -U postgres -h 127.0.0.1 --dbname=newdatabase dbname.tar

Connect to new database and check the data

psql -U postgres -h 127.0.0.1 newdatabase