{"id":22492,"date":"2023-10-15T04:53:23","date_gmt":"2023-10-15T04:53:23","guid":{"rendered":"https:\/\/linuxways.net\/?p=22492"},"modified":"2023-10-19T02:15:34","modified_gmt":"2023-10-19T02:15:34","slug":"backup-restore-postgresql-database","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/backup-restore-postgresql-database\/","title":{"rendered":"How to Backup &amp; Restore PostgreSQL Database"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<h2><strong>PostgreSQL Installation on Ubuntu<\/strong><\/h2>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> apt update<\/div><\/div>\n<p>Install the most recent PostgreSQL version from the Ubuntu default repository.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> postgresql<\/div><\/div>\n<p>The configuration of PostgreSQL is stored in the \u201c\/etc\/postgresql\/12\/main\u201d directory, while the default data location is \u201c\/var\/lib\/postgresql\/12\/main\u201d.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">$ <span class=\"kw2\">sudo<\/span> systemctl is-active postgresql<br \/>\n<br \/>\n$ <span class=\"kw2\">sudo<\/span> systemctl is-enabled postgresql<br \/>\n<br \/>\n$ <span class=\"kw2\">sudo<\/span> systemctl status postgresql<\/div><\/div>\n<p>Also, validate that the PostgreSQL server is ready to accept the client connections by doing the following:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> pg_isready<\/div><\/div>\n<p>After that, we can create the database and configure the database.<\/p>\n<h2><strong>Creating a Backup of a Single PostgreSQL Database<\/strong><\/h2>\n<p>The \u201cpg_dump\u201d 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.<\/p>\n<p>Next, log in as a Postgres user and execute \u201cpg_dump\u201d as shown in the following:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>pg_dump souvikdb <span class=\"sy0\">&gt;<\/span> souvikdb.sql<\/div><\/div>\n<p>Other output formats are also supported by \u201cpg_dump\u201d:<\/p>\n<p><strong>-F<\/strong>: Output format<\/p>\n<p><strong> \u201cc\u201d<\/strong>: Custom format archive file<\/p>\n<p><strong>\u201ct\u201d<\/strong>: Tar format archive file<\/p>\n<p>All formats are compatible with \u201cpg_restore\u201d.<\/p>\n<p>Let us see an example:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>pg_dump <span class=\"re5\">-F<\/span> c souvikdb <span class=\"sy0\">&gt;<\/span> souvikdb.dump<\/div><\/div>\n<p>Or<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>pg_dump <span class=\"re5\">-F<\/span> t souvikdb <span class=\"sy0\">&gt;<\/span> souvikdb.tar<\/div><\/div>\n<p>The \u201c-f\u201d options help to save the output.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>pg_dump <span class=\"re5\">-F<\/span> d souvikdb <span class=\"re5\">-f<\/span> souvikdumpdir<\/div><\/div>\n<p>We may restore the dump using \u201cpsql\u201d as demonstrated in the following:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>psql <span class=\"re5\">-f<\/span> all_pg_dbs.sql postgres<\/div><\/div>\n<h2><strong>Recovering or Restoring a PostgreSQL Database<\/strong><\/h2>\n<p>To restore a PostgreSQL database, we can use the \u201cpsql\u201d or \u201cpg_restore\u201d commands. The \u201cpsql\u201d command recovers the text files created by \u201cpg_dump\u201d, but \u201cpg_restore\u201d recovers a PostgreSQL database from a non-plain-text archive prepared by \u201cpg_dump\u201d (custom, tar, or directory).<\/p>\n<p>The following is an illustration on how to recover a plain text file dump:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>psql souvikdb <span class=\"sy0\">&lt;<\/span> souvikdb.sql<\/div><\/div>\n<p>A custom-format dump must be restored using \u201cpg_restore\u201d as demonstrated because, as already mentioned, it is not a script for \u201cpsql\u201d. Here, \u201cd\u201d means \u201cdirectory\u201d format archive:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>pg_restore <span class=\"re5\">-d<\/span> souvikdb souvikdb.dump<\/div><\/div>\n<p>&nbsp;<\/p>\n<h2><strong>Taking Backup of Large PostgreSQL Database<\/strong><\/h2>\n<p>We can perform a compressed dump by filtering the output of \u201cpg_dump\u201d 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:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>pg_dump souvikdb <span class=\"sy0\">|<\/span> <span class=\"kw2\">gzip<\/span> <span class=\"sy0\">&gt;<\/span> souvikdb.gz<\/div><\/div>\n<h2><strong>Remote PostgreSQL Database Backup<\/strong><\/h2>\n<p>The \u201cpg_dump\u201d 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:<\/p>\n<p>Replace the 30.10.20.10 hostip or hostname.<\/p>\n<p>Replace 5232 with database port.<\/p>\n<p>Replace the \u201csouvikdb\u201d database name.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>pg_dump <span class=\"re5\">-U<\/span> souvik <span class=\"re5\">-h<\/span> 30.10.20.10 <span class=\"re5\">-p<\/span> <span class=\"nu0\">5232<\/span> souvikdb <span class=\"sy0\">&gt;<\/span> souvikdb.sql<\/div><\/div>\n<p>Utilizing the \u201cpg_dump\u201d and \u201cpsql\u201d utilities as demonstrated. It is also feasible to directly dump a database from one server to another.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>pg_dump <span class=\"re5\">-U<\/span> souvik <span class=\"re5\">-h<\/span> 30.10.20.10 souvikdb <span class=\"sy0\">|<\/span> pqsl <span class=\"re5\">-U<\/span> souvik <span class=\"re5\">-h<\/span> 30.10.20.30 souvikdb<\/div><\/div>\n<h2><strong>Using a Cron Job to Auto Backup a PostgreSQL Database<\/strong><\/h2>\n<p>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.<\/p>\n<p>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:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">$ <span class=\"kw2\">mkdir<\/span> <span class=\"re5\">-p<\/span> <span class=\"sy0\">\/<\/span>srv<span class=\"sy0\">\/<\/span>backups<span class=\"sy0\">\/<\/span>databases<br \/>\n<br \/>\n$ crontab <span class=\"re5\">-e<\/span><\/div><\/div>\n<p>This new service is started automatically by the cron service without requiring a restart.<\/p>\n<p>It automatically takes the backup of our important files.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>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.<\/p>","protected":false},"excerpt":{"rendered":"<p>Tutorial on how to restore and back up the PostgreSQL databases using its key principles, approaches, and best practices to ensure the security of the data.<\/p>","protected":false},"author":111,"featured_media":22495,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-22492","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=22492"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22492\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/22495"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=22492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=22492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=22492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}