Starting with PostgreSQL in Docker: Setting up and creating a sample app.

✨ Starting PostgreSQL in a Docker container is as simple as creating a custom network and a volume. Once it’s up and running, connecting to it from the psql tool or a SQL client on your host OS is a breeze. And with a few extra steps, you can even connect from an application running in a Docker container. All in just a few minutes! 🐘🐳πŸ”₯ #DockerizePostgreSQL

🐘 Introduction

In this guide, we will explore the process of setting up PostgreSQL in a Docker container, connecting to the container using various SQL tools, and then creating a sample application to connect to the PostgreSQL instance.

Key Takeaways

Key PointDescription
Create a custom Docker NetworkAllows containers to communicate with each other easily
Set up a directory as a volumeEnables easy access to logs and data, preventing data loss upon container recreation

🐳 Starting PostgreSQL in Docker

To begin with, it’s essential to follow two extra steps before starting PostgreSQL in Docker. Firstly, creating a custom Docker Network using the docker network create command, and secondly, setting up a directory on the host operating system to be used as a volume by the PostgreSQL container. This volume, called "postgres volume", will store logs and data.

πŸ›  Configuring PostgreSQL Container

Using the docker run command, the PostgreSQL container can be started. By specifying custom parameters such as username, password, port number, and version of the Docker image, the container is up and running.

Important Note

Ensure that stronger passwords are used in production environments to enhance security.

πŸ’» Connecting to the PostgreSQL Container

The psql tool, packaged with every PostgreSQL distribution, is used to connect to the PostgreSQL container. With a simple command, users can connect to the PostgreSQL instance.

πŸ“Š Creating a Sample Database

A sample table, pizza_order, is created to store pizza orders. Additionally, a Common Table Expression is used to generate a dataset and populate the table with sample orders.

Sample Table Data

Order IDPizza Type
1FES
2Margarita
3Veo
4Pepperoni
5Margarita

πŸ–₯ Connecting from a SQL Tool

Connection to the PostgreSQL instance is also established from a SQL tool running on the host operating system. By utilizing the exposed port number, the SQL tool successfully connects to the PostgreSQL instance within the Docker container.

πŸš€ Creating a Sample Application

A sample application, written in Node.js, is created to connect to the PostgreSQL instance. After configuring the application and executing the connection logic, the application successfully connects, retrieves data, and demonstrates seamless connectivity.

Dockerizing the Application

The application is further Dockerized by creating a Dockerfile and running it as a container on the same network where the PostgreSQL instance is running. This enables the application to communicate with the PostgreSQL container using the custom Docker Network.

πŸŽ‰ Conclusion

In conclusion, we have successfully delved into the process of setting up PostgreSQL in a Docker container, establishing connections from various SQL tools, and creating a sample application to interact with the PostgreSQL instance. By following the steps outlined in this guide, users can master the art of managing databases within Docker environments.

FAQ

For detailed instructions on reproducing the steps mentioned in this guide, refer to the video description for additional resources.

Stay tuned for more exciting database tutorials!

About the Author

About the Channel:

Share the Post:
en_GBEN_GB