Quick Tip: PostgreSQL Dump Database and Restore PostgreSQL Database

Tags
4 October 2022
Complete Guide for CTO & IT Directors
Microservices under X-Ray Three books image Download free ebook

Making regular backups is an essential aspect of working with the PostgreSQL database in any production environment. No matter the size, you must take precautions to address potential data loss, and regular backups of the entire database are the best thing you can do. That way, in case of any problems with the database, such as data loss or corruption, you can simply load data from the latest version of the PostgreSQL backup file.

PostgreSQL includes native tools that can allow to easily dump, manage, and restore a PostgreSQL database.

How to dump a PostgreSQL database

PostgreSQL provides the pg_dump which is a utility that helps you efficiently do a backup of your database. It will generate an SQL database file which will make it easy to restore data should you need it.

To back up a PostgreSQL database you have to log in to your database server. Then switch to the Postgres user account and run pg_dump in the following manner:

pg_dump database_name > database_name.sql

It is generally a good habit to employ a system of database name building, as it’s good to be able to know what each individual database is. A good idea is to always include the date of the backup file so that when you want to restore PostgreSQL database you will know at a glance which one simply from the name of the database.

Changing the format of the PostgreSQL data dump

PostgreSQL database dump is, by default, a plain SQL script file, but this is not the only format that’s available as you can also do a custom format dump. You can specify the output file format with -F option (c = custom format archive file, d = directory format archive, t = tar file).

As an example you can do it like that:

pg_dump -F c database_name > database_name.tar

or:

pg_dump -F c database_name > database_name.dump

Specifying directory for the database dump

When dumping data in the directory output format, you can use -f flag to specify the target directory (instead of a file). Please mind, that the specified directory that the pg_dump will create must not exist. You can dump data into a specified directory by using the following command:

pg_dump -F d database_name -f database_directory	

Dumping all PostgreSQL databases

You can back up more than one database at a time. You can use pg_dumpall to dump all PostgreSQL databases.

pg_dumpall > all_databases.sql

Restoring a PostgreSQL Database

If you want to restore the desired database, you can use pg_restore (restores a PostgreSQL database from an archive created in one of the other formats such as tar or directory) or psql (restores the text files that were created by pg_dump).

The following command is the example of psql use:

psql database_name < database_name.sql

A custom format dump is not a SQL script file, so it must be restored with pg_restore. You use it like that:

pg_restore -d database_name database_name.dump 

or

pg_restore -d database_name database_name.tar

or like this for directory format archive

pg_restore -d database_name database_directory

pg_dump and psql utilities as shown.

Dealing with large PostgreSQL databases

If you want to back up a large database and you wish to get a smaller output file, you can compress the dump. However, you will have to filter the output format with a compression tool, e.g. gzip.

pg_dump database_name | gzip > database_name.gz

If your database is extensively large, you can do a parallel dump by dumping number_of_jobs simultaneously. We use -j flag for this:

pg_dump -F d -j 8 -f database_namedir

Please remember that though a parallel dump reduces the time of the database dump it will inevitably increase the load on the database server.

Latest Posts
web app speed

Revisiting Web App Speed

The performance of a web application can either encourage or deter user interest. Businesses should prioritize performance improvements to enhance the overall user experience and maintain user interest. Let’s delve into a mixture of development optimization, marketing, and… cognitive sciences? All for the sake of providing a smooth user experience. What Is Web Application Speed? […]

/
types of supervised learning

Types of Supervised Learning: A Look Into One of Key Branches of ML

Supervised machine learning, a pivotal branch within the vast domain of machine learning, represents a paradigm where machines are trained to decipher patterns and make decisions based on provided examples. This learning approach hinges on the use of labeled data – datasets where input data points (features) are paired with the correct output (target), thereby […]

/
software development stages

Software Development Life Cycle. How to Handle a Multi-Stage Software Development Process?

Creating a system that performs complex functions requires more than rock-solid expertise. You need a structured approach that will help you achieve your software development goals as efficiently as possible. Software development is a long, complex, and tedious process ridden with challenges. Common issues include incomplete requirements, changing project scopes, poor communication, unrealistic deadlines, insufficient […]

/
data science for finance

Data Science in Finance: Who is a Data Scientist and What They Do?

In the dynamic world of finance, staying ahead of the curve requires more than just traditional methods. As technology continues to evolve, the role of data science becomes increasingly crucial in deciphering complex financial landscapes. In this article, we’ll delve into the significance of data science in finance, its applications, the responsibilities of financial data […]

/
angular apps

Angular Apps: Top 7 Web Application Examples, Advantages, and Considerations

Angular is a leading development tool for building sophisticated web apps. Check out the top applications fueled by this Google-backed platform and learn about its strengths and weaknesses. Angular is a household name in the front-end development industry and the key competitor of React (aka ReactJS). As one of the leading web development frameworks, it […]

/
ux writing samples

UX Writing Samples. How to Enhance Usability With Effective Microcopy?

Text is an integral part of UI design and user experience. High-quality, usability-focused copy helps engage users and turn them into customers. User experience (UX) writing is much more than a buzzword. It combines writing proficiency and inventiveness with a strong focus on user actions. The goal is to make things smooth, easy, and informative […]

/
Related posts
web app speed

Revisiting Web App Speed

The performance of a web application can either encourage or deter user interest. Businesses should prioritize performance improvements to enhance the overall user experience and maintain user interest. Let’s delve into a mixture of development optimization, marketing, and… cognitive sciences? All for the sake of providing a smooth user experience. What Is Web Application Speed? […]

/
software development stages

Software Development Life Cycle. How to Handle a Multi-Stage Software Development Process?

Creating a system that performs complex functions requires more than rock-solid expertise. You need a structured approach that will help you achieve your software development goals as efficiently as possible. Software development is a long, complex, and tedious process ridden with challenges. Common issues include incomplete requirements, changing project scopes, poor communication, unrealistic deadlines, insufficient […]

/
angular apps

Angular Apps: Top 7 Web Application Examples, Advantages, and Considerations

Angular is a leading development tool for building sophisticated web apps. Check out the top applications fueled by this Google-backed platform and learn about its strengths and weaknesses. Angular is a household name in the front-end development industry and the key competitor of React (aka ReactJS). As one of the leading web development frameworks, it […]

/
cert pinning android

Mobile Development and Security: Certificate Pinning on Android

In today’s increasingly interconnected digital world, the importance of security for mobile apps and web services cannot be overstated. As cyber threats evolve, so must the defenses and measures we deploy to safeguard sensitive data and maintain trust. One of the pivotal practices in enhancing network security is certificate pinning, a technique that ensures a […]

/
django apps

Django Apps, Projects, and Other Caveats

Django, emerging as a significant player in the realm of web frameworks, stands out as a Python-based toolset that revolutionizes the way developers approach web application development. It is not merely a framework but a holistic environment that encapsulates a developer’s needs for building robust, efficient, and scalable web applications. Born out of a practical […]

/
Angular mobile apps

Angular Mobile Apps: Key Advantages and Considerations

Angular is a popular framework backed by a huge community, favored by many developers, and used by leading corporations, including Google, Microsoft, and Samsung. Find out how it can streamline mobile app development. In software development, efficiency is paramount. Building maintainable and scalable applications requires a lot of proficiency and experience, which enable you to […]

/
Talk with experts

We look forward to hearing from you to start expanding your business together.

Email icon [email protected] Phone icon +1 (888) 413 3806