Docker PostgreSQL: A Beginner's Guide to Running Postgres with Ease

Docker PostgreSQL: A Beginner's Guide to Running Postgres with Ease

Mastering Containerization: Step-by-Step Tutorial for Setting Up PostgreSQL with Docker for New Developers

Prerequisite

  1. You should have docker installed, here is official docs on how to install follow it

    %[docs.docker.com/get-docker/]

  2. You should have a Docker hub its free.

That was all the prerequisites we need, now let's get started

1. Login to the docker hub in the terminal (optional)

Sometimes docker tells you to login before downloading an image if that the case then follow this step to log in using terminal

open your terminal and type following command, it will walk you through the to login process, you just have to enter your username and password so don't worry

docker login

2 . Pull the Postgres docker image from the Docker hub

What is Docker hub ?

it's like a play store or marketplace where you can find or upload your own docker images

go to Docker hub or type the following command to install Postgres latest version to your system

docker pull postgres

or, if you need a specific version you can specify that

docker pull postgres:15.3

3.Running the Postgresql

In this section, we will see how to run Postgres and access it on localhost

following is the command that we will need to execute to run Postgres

docker run --name anyName -e POSTGRES_PASSWORD=123321 -d -p 5400:5432 postgres

By using this command, you'll start a PostgreSQL container named "anyName" set the password to "123321" for the default user "Postgres," and expose the container's port 5432 (PostgreSQL default) to your host machine's port 5400.

Let's understand each component of this command so that we can modify it asper
our need.

  • --name <cusotmNmae> This command is used to set a custom name to the running instant this will be used as username for your docker database.

  • -e This flag is used to set environment variables for that container here we have set -e POSTGRES_PASSWORD=123321 a password for ower DB via an environment variable.

  • -d This tells docker to run the container in detached mode that is to run in the background.

  • -p This flag forward the port from the docker container to our localhost

    syntax of this is -p <yourPort>:<containerPort> in our case we have forwarded containers 5432 to our 5400 by 5432 is the default port of Postgres.

TODO: add an image od the breakdown

Thats it, now you can access the database using any client with the credentials you gave above.

4* Important Docker command that will help further

Start the container with the same configuration and data that it was running

docker start <id> : so you want to restart the container that we created in this tutorial type docker start 1234567890 put id of container in place of 1234 you can get that by the below command

list the current running container

docker ps : list current running container, from here you can get info like id, name and port at which it is available

list current and previously running container

docker ps -a : This will list all container that is running now or was running previously from here you can copy the id and paste to the next command to restart that container with the same credentials and data as it was created.

That's it, now you can freely roll up and create as many PostgreSQL databases as you want. You can confidently run PostgreSQL in docker,

Shaun The Sheep Yes GIF by Aardman Animations

Thanks for reading do give it a heart and share your learning in socials.

Some Resources to get started, docker