How To Backup PostgreSQL Databases On An Ubuntu VPS - DigitalOcean

You might also like

Download as pdf
Download as pdf
You are on page 1of 8
4) Community = TUTORIAL How To Backup PostgreSQL Databases on an Ubuntu VPS Ubuntu PostgreSQL Backups By Justin Ellingwood Posted August 28,2013 @ 381k What is PostgreSQL? PostgreSQL is a modern database management system. It is frequently used to store and manipulate information related to websites and applications. ‘As with any kind of valuable data, itis important to implement a backup scheme to protect against data loss. This guide will cover some practical ways that you can backup your PostgreSQL data. We will be using an Ubuntu 12.04 VPS with PostgreSQL 9.1. Most modern distributions and recent versions of PostgreSQL will operate in a similar way. How to Back Up a PostgreSQL Database Using pg_dump PostgreSQL includes a utility called "pg_dump' that can be used to dump database information into a file for backup purposes. The pg_dump utility is run from the Linux command line. The basic syntax of the command is: pg_dump name_of database > name_of_backup_file The command must be run by a user with privileges to read all of the database information, so it is run as the superuser most of the time. For a real-world example, we can log into the "postgres" user and execute the command on the default database, also called "postgres": sudo su - postgres pe_dump postgres > postgres_db.bak This command is actually a PostgreSQL client program, so it can be run froma remote system as long as that system has access to the database. Ifyou wish to backup a remote system, you can pass the "-h" flag for specifying the remote host, and the "=p" flag to give the remote port: pg_dump -h remote host -p remote_port name_of_database > name_of_backup_file You can also specify a different user using the "-U" option if necessary. The syntax would be: pe_dump -U user_name -h remote host -p remote port name_of database > name_of_backup_file Keep in mind that the same authentication requirements exist for pg_dump as for any other client program. This means that you must ensure that your log in credentials are valid for the systems you are trying to back up. How to Restore Data Dumps from pg_dump with PostgreSQL To restore a backup created by pg_dump, you can redirect the file into psql standard input: psql empty database < backup_file Note: this redirection operation does not create the database in question. This must be done in a separate step prior to running the command. For example, we can create a new database called "restored_database" and then redirect a dump called "database.bak" by issuing these commands: createdb -T template® restored_database psql restored_database < database. bak The empty database should be created using "template0" as the base. Another step that must be performed in order to restore correctly is to recreate any users who own or have grant permissions on objects within the database. For instance, if your database had a table owned by the user "test_user’, you will have to create it on the restoration system prior to importing: createuser test_user psql restored_database < database. bak Dealing with Restoration Errors By default, PostgreSQL will attempt to continue restoring a database, even when it encounters an error along the way. In many cases, this is undesirable for obvious reasons. It can be painful to try to sort out what operations are needed to restore the database to its proper state. We can tell PostgreSQL to stop on any error by typing: psql --set ON_ERROR_STOP-on restored database < backup_file This will cause a PostgreSQL restore operation to halt immediately when an error is encountered. This will still leave you with a crippled database that hasn't been fully restored, but you can now handle errors as they come up instead of dealing with a list of errors at the end. A better option in many situations can be the "-1" (the number one) or "--single- transaction” option: psql -1 restored database < backup file This option performs all of the restoration details in a single transaction. The difference between this option and the "ON_ERROR_STOP" setting is that this will either succeed completely or not import anything This can be a costly trade-off for larger restorations, but in many cases, the benefit of not leaving you with a partially restored database heavily outweighs that cost. How to Backup & Restore All Databases in PostgreSQL To save time, if you would like to backup all of the databases in your system, there is a utility called "pg_dumpall’. They syntax of the command is very similar to the regular pg_dump command, but it does not specify the database. Instead, the command backs up every available database: pg_dumpall > backup_file You can restore the databases by passing the file to psql, with the default database: psql -# backup file postgres Conclusion Backups are an essential component in any kind of data storage plan. Fortunately, PostgreSQL gives you the utilities necessary to effectively backup your important information As with any kind of backup, itis important to test your backups regularly to ensure the copies that are created can be restored correctly. The backups you create are only useful if they can actually be used to recover your system. By Justin Ellingwood By Justin Ellingwood Posted August 28, 2013 © 381k Was this helpful? VHYO Report an issue Related TUTORIAL TUTORIAL How To Install and How To Secure Nginx Configure Postfix on with Let's Encrypt on Ubuntu 20.04 Ubuntu 20.04 Postfix is a popular open- Let's Encryptis a source Mail Transfer Agent Certificate Authority (CA) (MTA) that can be used to that provides an easy way route and deliver email on to obtain and install free TUTORIAL TUTORIAL How To Serve Flask How To Install and Applications with Configure Laravel with Gunicorn and Nginx on Nginx on Ubuntu 20.04 Ubuntu 20.04 L aravel is an open-source In this guide, you will build PHP framework that a Python application using provides a set of tools and the Flask microframework resources to build modern Still looking for an answer? &$8 Aska question Q__ Search for more help 7 Comments eb A B ° Leave a comment... eRe +, arosemenae October 16,2013 this is the script that i use to backup my database, it creates a backup with a timestamp and keeps the latest 14 backups, i run it on a daily cron so this script keeps a 2week daily backup of the database Is -t *.sql | sed -e ‘1,13d" | xargs echo Done at “date +\%V-\%m=\%d_\%T” pg_dump dbname --username=dbuser > “date +\XY-\%m-\%d_\&T° .sql ‘\n" rm Reply Report % rahul.dimri June 30, 2014 HiAll, | got similar requirement in one of my project, Where | need to take back and stored the data on remote directory. Could you please let me know Do | need to modify this above script Reply Report & Araknor Decemberts 2014 > Found a handy script here: https://gist github.com/matthewlehner/3091458 Looks like someone was copying the automysqlbackup script and making it work for PostgreSQL. Seems to work! Reply Report 2, islammanjurul January 9,206 Hi, can you help to upgrade postgres to newer version. My db is now on 9.4, want to upgrade to 9.5 and to make sure all my existing database, tables, and pg configurations and settings are transferred to newer one. | am looking for a detailed guide Reply Report 2 devmtl123 August 16,2016 When | use psql -f backup_file postgres | got all kinds of errors like these: psql:2016-08-14 @2h5@n_S@_pg-master.bak:15427: psql: 2016-08-14 @2hS@m_S@_pg-master.bak:15435: psql:2016-08-14 @2h5@n_S@_pg-master.bak: psql: 2016-08-14 @2hS@m_S@_pg-master.bal psql: 2016-08-14 @2hS@m_S@_pg-master-bal psql: 2616-08-14 @2hS@m_S@_pg-master.bak:15467: psql: 2016-08-14 @2hS@n_5@_pg-master.bak:15475: psql: 2016-08-14 @2hS@m_5@_pg-master.bal psql: 2016-08-14 @2hS@n_S@_pg-master.bal psql:2016-08-14 @2h50n_5@_pg-master.bak: psql: 2016-08-14 @2hS@m_S@_pg-master.bak:15507: psql:2016-08-14_@2hS@m_S@_pg-master.bak:15515: multiple primary keys for ta relation “accesstokens_token multiple primary keys for ta relation "app_settings key _u multiple primary keys for ta relation “apps_name_unique" multiple primary keys for ta relation “apps_slug_unique" multiple primary keys for ta relation "clients_name_uniqu multiple primary keys for ta relation “clients_slug_uniqu SoI need to manually delete each individual database !! It drives me crazy. How can | force an overwrite ?? I'm using version 9.4.18 within a Docker container. Cheers! Pp Reply Report G Olek September 2018 I'm using Pg_dump if | need to backup PostgreSQL database on Linux, it’s really the best way. But sometimes | have to work with PostgreSQL on Window, and in that case, | use the simple tool to backup Postgres database - PostgreSQL-Backup http://postgresql- backup.com/postgresql-blog/backup-postgres-database . I wish to use PostgreSQL- Backup to backup Postgres databases on Linux. Reply Report +, amayash6777 April 30, 2019 how to download backup file to local computer from digital ocean Reply Report This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. BECOME A CONTRIBUTOR You get paid; we donate to tech nonprofits.

You might also like