Instantly set up your PostgreSQL DB with Docker.
Well, you must have read the title and must have known in advance that Docker is an epic sh*t, but this is what Docker really is.
According to Wikipedia, “Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. The software that hosts the containers is called Docker Engine.”
Complex stuff right? But to sum it up, Docker is basically a way to create containers of anything, these are isolated from the outside environment and can be run using Docker Engine.
But then, you might ask, why TF do we need a container for? Now, according to Docker, “A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application.”
Basically, Docker images contain everything in the universe to run the required code bundled in the container, which means, your guy James can’t complain that the code isn’t running on his device, if it runs on your device, it runs on every device.
Then why worry about setting up Postgres in Docker?
Well,
- Setting up Postgres the connection in WSL is time-consuming.
- You will encounter scenarios where you’ll need to bundle in the PostgreSQL db along with the codebase inside a container, and then, you will search for this article.
Let’s begin the setup
Setting up PostgreSQL inside Docker is a piece of cake, but as a prerequisite, one must have Docker installed within their systems.
Here’s a link to proceed with the installation.
Now that you’re done installing Docker, let’s begin setting up PostgreSQL in our local Docker environment.
And not just PostgreSQL, you can install a whole bunch of different image repositories. Visit this site for further reference.
Let’s continue setting up PostgreSQL.
Step 1: Run the following command in the terminal.
docker run -p 5432:5432 -d \
-e POSTGRES_PASSWORD=admin \
-e POSTGRES_USER=admin \
-e POSTGRES_DB=mydb \
-v pgdata:/var/lib/postgresql/data \
postgres
In the above command, we are creating a process to run at port 5432, a common port for PostgreSQL to run across different platforms.
Followed by three environment variables which the Postgres image repository will understand and implement for us.
Next is the location where the data is supposed to be stored, it enables persistence, i.e. data is not lost when the image is terminated.
The image will then keep running in the background at port 5432.
Step 2: Get the container ID of the image.
In order to run the container, we need to selectively target it, and to do so, we require its ID.
To get the ID of a running container run the following command in the terminal.
docker container ls
It will list all the running containers, here is a reference output.
Now, copy the container ID corresponding to the image Postgres and save it for future reference.
Step 3: Execute the container.
The final step involves executing the container, type the following command in the terminal to begin the execution.
docker exec -it c9dca86bf618 psql -U admin mydb
Replace your container ID after the -it argument.
Here, admin is the username and mydb is the name of the database, this information was provided in the form of environment variables in step 1.
Now, at the end, it should open the CLI tool for Postgres, you can even use this instance of Postgres inside PgAdmin and other tools by using the port number.
Thanks for staying along 🎉🎉🎉, hope this method worked out for you and saved you a lot of time, comment down below and let me know what you think about docker and its power to practically run any image in its image repository !