Ubuntu 24.04
Sponsored Link

PostgreSQL 16 : Backup and Restore2024/05/30

 

For Backup and Restore PostgreSQL Data, it's possible to run with [pg_dump] and [pg_restore].

[1] Backup Databases.
# available types for [--format=*]
#     p = plain (SQL)
#     c = custom (compression)
#     t = tar
#     d = directory

# [ubuntu] user takes backup of [testdb]
ubuntu@www:~$
pg_dump -U ubuntu --format=t -d testdb > pg_testdb.tar

ubuntu@www:~$
total 8
-rw-rw-r-- 1 ubuntu ubuntu 6656 May 30 03:55 pg_testdb.tar


# admin user [postgres] takes backup of all databases

postgres@www:~$
mkdir ~/backups

postgres@www:~$
pg_dumpall -f ~/backups/pg_DB_all.sql

postgres@www:~$
ls -l ~/backups

total 4
-rw-rw-r-- 1 postgres postgres 3382 May 30 03:58 pg_DB_all.sql
[2] Restore Databases.
# [ubuntu] user restores [testdb] database from backup file

ubuntu@www:~$
pg_restore -U ubuntu -d testdb pg_testdb.tar

# admin user [postgres] restores all database from backup file
# if the type of backup file is SQL text, use [psql] command for restoring

postgres@www:~$
psql -f ~/backups/pg_DB_all.sql

Matched Content