Protecting the integrity of databases is essential for organizations of all kinds in the data-driven world of today. An open-source, dependable relational database management system called PostgreSQL is widely used in many different sectors. Knowing how to backup and restore a PostgreSQL database is a key skill that protects us from data loosing whether we are a novice or an expert database administrator.
Let us discuss about how to restore and back up the PostgreSQL databases. We will explain the key principles, approaches, and best practices to ensure the security and availability of our data.
Install the most recent PostgreSQL version from the Ubuntu default repository.
The configuration of PostgreSQL is stored in the “/etc/postgresql/12/main” directory, while the default data location is “/var/lib/postgresql/12/main”.
$ sudo systemctl is-enabled postgresql
$ sudo systemctl status postgresql
Also, validate that the PostgreSQL server is ready to accept the client connections by doing the following:
After that, we can create the database and configure the database.
The “pg_dump” program from PostgreSQL makes it easy to back up the databases. With the use of SQL commands, it creates a database file that can later be readily recovered.
Next, log in as a Postgres user and execute “pg_dump” as shown in the following:
Other output formats are also supported by “pg_dump”:
-F: Output format
“c”: Custom format archive file
“t”: Tar format archive file
All formats are compatible with “pg_restore”.
Let us see an example:
Or
The “-f” options help to save the output.
We may restore the dump using “psql” as demonstrated in the following:
To restore a PostgreSQL database, we can use the “psql” or “pg_restore” commands. The “psql” command recovers the text files created by “pg_dump”, but “pg_restore” recovers a PostgreSQL database from a non-plain-text archive prepared by “pg_dump” (custom, tar, or directory).
The following is an illustration on how to recover a plain text file dump:
A custom-format dump must be restored using “pg_restore” as demonstrated because, as already mentioned, it is not a script for “psql”. Here, “d” means “directory” format archive:
We can perform a compressed dump by filtering the output of “pg_dump” through a compression tool like gzip or any of our favourites if the database which we are backing up is huge and we want to create a somewhat smaller output file. So, use the following format:
The “pg_dump” is a standard PostgreSQL client tool that allows us to perform the operations on remote database servers. In addition, use the -U parameter to indicate the database role to connect as:
Replace the 30.10.20.10 hostip or hostname.
Replace 5232 with database port.
Replace the “souvikdb” database name.
Utilizing the “pg_dump” and “psql” utilities as demonstrated. It is also feasible to directly dump a database from one server to another.
Using cron tasks, we can schedule the backups at regular intervals. Cron jobs are a popular method to schedule various types of tasks to execute on a server.
The following cron job can be used to automate the PostgreSQL database backup. It should be noted that the commands that follow must be run as the PostgreSQL superuser:
$ crontab -e
This new service is started automatically by the cron service without requiring a restart.
It automatically takes the backup of our important files.
Integrity protection for PostgreSQL databases is essential. A reliable backup and restore plan are necessary. We can successfully handle the backup and restore the difficulties with the help of this guide. Data availability and recoverability are ensured through routine backups, strategic planning, and adherence to best practices to investigate the PostgreSQL backups, rely on tools and methods, and improve our data management.
Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…
Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…
At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…
I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…
Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…
Now I want to share the DNS server installation process on your Ubuntu 20.04 server.…