From the course: Database Foundations: Administration

Use pg_dump in the Postgres container

From the course: Database Foundations: Administration

Start my 1-month free trial

Use pg_dump in the Postgres container

- [Instructor] It's always a good idea to know alternative methods for working with a database server. While graphical interfaces can simplify many procedures, they add a layer between you and the server that can sometimes break down. Because of this, it's important to get comfortable with the native command line tools. Instead of using the Azure Data Studio graphical user interface for creating a PostgreSQL database backup, we can send our instructions directly to the server using a text interface. If you're on Mac OS or Linux, you can use the terminal application. If you're on windows, you can use the PowerShell application. These are the same applications that we use to initially set up our Docker containers. The other option is to use the built-in terminal window, right here inside of Azure data studio. It doesn't matter which method you choose, they'll all give you access to the same tools and give you the same results. I'm going to stay here inside of Azure data studio and come up to the view menu and use its terminal. Then over here on the far right, I'll just click on this upward arrow to maximize the view. Now when you start it up, it'll place you at the command prompt on your local file system. For me, it's inside of my windows user folder. The first thing that we need to do is move inside of the Docker container. We do that with the command Docker E X E C Then the flags dash I T followed by the name of the server we want to go inside of. Our Docker container is called PostgreSQL. Then we're going to use the bash shell to send commands inside of the terminal. When I press enter that moves me inside of the Docker container and it tells me that I'm at the root folder. Now we're working with the Linux file system inside of the container. So if you issue the DIR command it'll list out the folders and the files that exist in the container. Let's make a new folder to store our database backups. We do that with MK DIR and then the name of the folder. I'll call it backups. Now we can move into the backups folder with CD backups. Now I'll type in the DIR command again to view the backups folder and you can see that it's currently empty. Now we can use the PG dump command to create the database backup and store it inside of this new folder. I'll type in PG underscore dump. Then some options, dash capital U will specify the user account that we want to use for logging into our PostgreSQL database server. The user account is simply postgres. Then we need the name of the database that we want to make the backup of. That's the two trees DBF 4 database. Next we need a greater than symbol to specify the file location for the export of the backup file. I'm going to place this inside of our backups folder that we just created. So I'll type in a forward slash backups. Then another forward slash and the name of the output file. Let's call this two trees backup dot SQL I'll press enter and the PG dump application creates a database backup and exports it to the file that we just specified. Now, PG dump has a lot of configuration options. As we saw with the gooey approach. You can certainly dive into the documentation to see how to implement all of those using the command line but this simplified command will give us a full database backup with all the default options, which is perfect for our purposes. Now let's verify that the backup file was created. I'll type in the DIR command again to explore the backups folder. And there is the file inside of that folder. Finally, we just need to get this backup file out of the container so that we can work with it. Let's exit out of the container. This places us back in our own computer's file system. And you can see I'm inside of my user's home folder. Now we'll use the Docker CP command to copy a file from inside the container to somewhere else on our computer. I'll say Docker CP the name of the container, PostgreSQL, followed by a colon. Then the file path to the file inside of the container and that is slash backups slash two trees backup dot SQL. Then a space and the file path on our local computer where you want to copy it to. I'll place it into my desktop. For that, I'm going to go C colon backslash users backslash my user account name, backslash desktop, backslash two trees backup dot SQL. If you're on Mac OS or Linux, you're going to use tilda forward slash desktop to get to the same relative location. I'll press the enter command, oh and it looks like it had a typo in the file pass so I'm going to press the up key to go back to the last command. And it's actually just W I L B E for my personal user accounts desktop folder. So I'll make that change there, press enter. All right, that time it copied the file without any issues. Now we can minimize the Azure data studio interface and here on my desktop is the backup file that I just copied out of the Docker container. I'll double click on it to open it. And you can see here is all of the texts that it contains. It tells me it's a full database dump out of my PostgreSQL database server. So that's the backup file. Using the PG dump command line tool, we're able to create the backup. Then using some Docker commands, we're able to move it off of the server's container and into our own file system where we can open it, explore its contents and archive it with all of our other documents.

Contents