How to Set Up a PostgreSQL Database with Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
In this video, we'll look at how to set  up a PostgreSQL database using Docker. Let's get started. Here are the high level steps we need to do. First, we download the Docker application. Next, we set up an account on Docker Hub. Then, we run Docker and  download the PostgreSQL image. Then, we run the PostgreSQL image. Finally, we connect to the  database and run some SQL. This video is demonstrated on a Mac but  should work in a similar way on Windows. To get started, we need to  download the Docker application.   This is freely available from the  Docker website, at www.docker.com. Once the website has loaded, click on Get Started. On the Get Started page, there are several  options. Click on Docker Desktop. The other   two options, Docker Hub and Play with  Docker, are not needed for our process. Select the right version for your computer,   which is either of the two Mac  versions or a Windows version. The Docker setup file will then begin downloading.   It's about 580 MB which could  take a few minutes to download. Once the download is complete, install it.  This can be done by opening the file on MacOs   and dragging it to the Applications folder, or  following the installation steps on Windows. It takes a couple of minutes to install. Once it is installed, you can run it. This  will cause the Docker service to start.   I believe you can also run it from the  command line or from elsewhere in Mac,   but this is the approach that has worked  for me and will hopefully work for you. Now we've got Docker running, it's  time to get our database files. The files that Docker uses to run applications  are called images, which are a pre-built   collection of files. These images are  available on a website called Docker Hub. There are a whole range of images there, but  the one we're looking for is for PostgreSQL. So, we'll need to visit the Docker  Hub website, set up an account,   find the right image, and then start  using it. Let's see how we can do that. First, visit the Docker Hub  website at hub.docker.com. You'll need an account to access the Docker  images, so click on the Sign Up button. Enter your details on the  Sign Up page. Remember the   password you enter here as you'll need it later. The next page is the Choose Your Plan  page. There are a couple of options,   but for now you can select the Free  plan. This is enough for our purposes. Follow the steps to verify your email address.  You now have an account on Docker Hub. Now we've logged in to Docker Hub, it's time  to find the image for the PostgreSQL database.   We're going to get the URL for the image,   and then use this URL as part of a  command we'll run on the command line. To find the image, enter the term "postgres"  into the search bar at the top of the page.   You can enter "postgresql" instead,  and you'll see the same results. Click on the postgres entry here,  and you'll be taken to this page. If you scroll down a little, you can see a  few sample commands under the "How to use   this image" section. Copy the command from the  first example here, which says "docker run",   and is a template you can edit before running. There are a few things to note in this command. There are two dashes then a name  keyword, then some-postgres.   This "some-postgres" is the name of the  image, which you can change if you like.   This name is what's used when you start  and stop the image on your computer. The -e refers to environment variables.  There is only one in this command,   and it's called postgres_password. This is where  we set the root password. In this example, it's   specified as "mysecretpassword". This is something  you can also change before you run the command,   and I'll show you how to do that shortly. The "-d" means detached mode,   which means the container runs in the background  and we can use our terminal for other commands. The final part is the path to the image  on docker hub, which will be downloaded.   This is the word "postgres". You can  add tags after this word postgres,   if you want a specific version.  But for now, we don't need to. So, copy this command. We'll make a couple of  modifications to this command before we run it,   which you may find easier to do  if you paste it into a text editor At the top is our command from the Docker  Hub page. We need to make two changes. The only thing you have to change  is to add a parameter of "-p",   which is short for port. We then add two numbers,  separated by a colon. This will expose, or open   the port from the Docker container to our main  operating system, or our host operating system.   This will allow us to use a program such as  DBeaver to connect to the database. If we   leave this out, then we'll have to run commands  on the Terminal to run SQL, which is not ideal. You can change the name of the image, which is  some-postgres here, to something else if you want.   And you can change the postgres  password to something else.   But the default values in the  command will work just fine. Now we have our command  updated, we need to run it.   To do this, we need to open a command line  or terminal. I'll open the Terminal app   as I'm on a Mac, but I believe the Command  Line on Windows will work in the same way. Once the Terminal app is open, we can start  the process of getting our Docker image.   We're going to do three things: log in,  then download the image, then run it. We need to log in to the Docker Hub account first.  This is done using the docker login command. Enter "docker login" at the command line  and press enter. You'll be asked to enter   your username and password, which is what you  provided when you created the Docker Hub account. Now we have logged in, we can download the image.  This can be done using the "docker pull" command,   which is the command in the black  box we saw on the Docker Hub page.   However, we can also use the "docker run"  command, which will run the container if   it exists on your computer already,  and download it if it does not exist. So, paste the command here in the terminal window  that you updated from earlier in this video. Then, press Enter to run the command. The image will start downloading.  It may take a few minutes. Once the docker run command has completed,   the image will start up. This  can take a minute or two. If you get an error about the name already  being used, then it's likely because you have   run this command in the past and the container  name of "some-postgres" is already created.   If so, you can start the existing container.  To do this, type "docker start some-postgres". After a moment, the image will start up. To check the status of your containers,   you can run the command "docker ps". This  shows a list of all images that are running. In the status column, you'll see something  like "starting" to indicate the container   is starting up. Run the docker ps command  again in a minute or two and you should   see "healthy" or "up 10 seconds".  This means the container is running. Awesome! We now have a Docker container  running with our PostgreSQL database.   Let's connect to it and run some SQL. We'll use DBeaver to connect, which is a freely  available IDE that works on Windows and MacOS.   You can use whatever IDE you want.  The process should be similar but   the screens will just look different in each IDE. For now, I'll assume you have an IDE  ready, such as DBeaver, so open it now. Click Create a new connection  and you'll see this window here. Select Postgres, and click Next The connection details are available  on the image page on Docker Hub   and are also in the command we  ran, and we'll enter them here. For the host, enter localhost. For the port, set it to 5432, which is what  we added to the docker run command earlier. For the username, enter "postgres". For the password, enter the password you provided  when you ran the terminal command earlier.   By default, this is mysecretpassword. Leave everything as the default, and click  Test Connection. If everything has been   set up correctly, the connection should  be successful. If there are any errors,   take a look at the description and comments  where I'll list a few ways to resolve them. You can then click OK to save  the connection. Click on the   new connection on the grid here, and after a  moment, you'll be connected to the database. You should see a screen that looks like  this. A new SQL tab will be displayed. Enter a simple query, such as  selecting the current date, and run it.   This should show the current date and time. You'll see the output on the screen here. Congratulations! You've successfully  set up a PostgreSQL database inside   a Docker container and connected to it! If you have any errors, let me know  in the comments below. I'll put some   resources in the Description  for some common issues as well. To stop the database, you can run the Docker  Stop command. Go back to the terminal,   and enter "docker stop some-postgres", where  some-postgres is the name of your container. This will stop the container  and the PostgreSQL database.   To start it again, use the docker  run command we saw earlier. That brings us to the end of this  tutorial. I'll create some other   videos to cover things like persisting  data and automatically running scripts. If you learned something new from this  video, make sure to subscribe to my channel. If you want to learn more about  database design and development,   visit databasestar.com. That’s where I  share my best database-related content. Which part of this Docker tutorial was the most  helpful for you? Was it the command to actually   run the container? Or the steps to connect to  the database using your IDE? Or something else? Thanks for watching.
Info
Channel: Database Star
Views: 154,653
Rating: undefined out of 5
Keywords: docker postgresql, docker database, postgresql docker, postgresql
Id: RdPYA-wDhTA
Channel Id: undefined
Length: 10min 1sec (601 seconds)
Published: Sun Aug 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.