Dockerizing Your PostgreSQL Database: A Beginner's Guide

Photo by Ian Taylor on Unsplash

Dockerizing Your PostgreSQL Database: A Beginner's Guide

Are you tired of managing your PostgreSQL database on your local machine? Want to make it easier to develop and test your application with a PostgreSQL database? Look no further than Docker!

Docker is a containerization platform that allows you to package your application and its dependencies into a self-contained unit called a container. This makes it easier to develop, test, and deploy your application, as you can be sure it will run the same way on any machine with Docker installed.

This tutorial will show you how to use the official PostgreSQL Docker image to run a PostgreSQL database in a Docker container. We will cover the following topics:

  1. How to pull the PostgreSQL Docker image

  2. How to start a PostgreSQL container using the image

  3. How to access the PostgreSQL shell and run SQL commands

  4. How to install the PostgreSQL client utilities on your local machine

  5. How to connect to the PostgreSQL server from your local machine

Let's get started!

Installing Docker

First, you will need to have Docker installed on your machine. You can download Docker from the official website (docker.com) and follow the installation instructions for your operating system.

Pulling and Running the Postgres image

Once you have Docker installed, open a terminal and run the following command to pull the PostgreSQL Docker image:

docker pull postgres:latest

This will download the latest version of the PostgreSQL Docker image to your machine.

Next, run the following command to start a PostgreSQL container using the image you just pulled:

docker run --name postgresdb -p 5432:5432 -e POSTGRES_PASSWORD=password -d postgres

This will start a PostgreSQL container with the name "postgresdb" and expose port 5432 on the container to port 5432 on the host machine. It will also set the password for the "postgres" user to "password".

Accessing the Postgresql Shell

To access the PostgreSQL shell and run SQL commands, run the following command:

docker exec -it postgresdb psql -U postgres

This will open the PostgreSQL shell, where you can enter SQL commands to interact with the database.

To exit the PostgreSQL shell, run the following command:

\q

To stop the PostgreSQL container, run the following command:

docker stop postgresdb

Connecting to your PostgreSQL container from a psql Client

Now that you have a PostgreSQL database running in a Docker container, you may want to connect to it from your local machine. To do this, you must install the PostgreSQL client utilities on your local machine.

On Ubuntu, you can install the PostgreSQL client utilities by running the following command:

sudo apt-get install postgresql-client

On other operating systems, you will need to follow the appropriate installation instructions for your system.

Once the PostgreSQL client utilities are installed, you can connect to the PostgreSQL server from your local machine using the psql command-line interface. To do this, run the following command:

psql -h localhost -U postgres

This will open the psql command prompt, where you can enter SQL commands to interact with the PostgreSQL server.

That's it! You now know how to use the official PostgreSQL Docker image to run a PostgreSQL database in a Docker container.

Conclusion

This article showed you how to use a Postgresql Docker container as a Postgres database and how you can connect to the container in the same way you will connect to a local PostgreSQL server. If you found it helpful, give it a few reactions and subscribe to my newsletter. Thanks for reading. Adios ✌🏾🧑.

Β