In this tutorial, we will explore how to connect to a PostgreSQL database running inside a Docker container from outside the container. This can be useful when you want to access your database from a local machine or another server.
Prerequisites
- Docker installed on your system
- PostgreSQL image pulled from Docker Hub (or any other registry)
- A basic understanding of Docker and PostgreSQL concepts
Running PostgreSQL in a Docker Container
To start, we need to run the PostgreSQL container. We can do this using the following command:
docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
Let’s break down what each part of this command does:
--name my-postgres
gives our container a name, so we can easily identify it later.-e POSTGRES_PASSWORD=mysecretpassword
sets the password for the PostgreSQL user. You should replacemysecretpassword
with your own secure password.-d
runs the container in detached mode, meaning it will run in the background and not block our terminal.-p 5432:5432
maps port 5432 on the host machine to port 5432 inside the container. This allows us to connect to the PostgreSQL database from outside the container.
Connecting to the Database
Now that our container is running, we can connect to the database using a tool like psql
. We can do this by running:
psql -h localhost -p 5432 -U postgres
Here, we’re telling psql
to:
- Connect to the host
localhost
(which is where our container is running) - Use port
5432
(which we mapped earlier) - Authenticate as the user
postgres
You will be prompted for a password, which should be the same one you set when running the container.
Creating a Database and Tables
Once connected to the database, you can create a new database and tables using standard SQL commands. For example:
CREATE DATABASE mydatabase;
\c mydatabase
CREATE TABLE mytable (id SERIAL PRIMARY KEY, name VARCHAR(50));
INSERT INTO mytable (name) VALUES ('John Doe');
SELECT * FROM mytable;
Persisting Data
By default, any data you create in the database will be lost when the container is restarted or deleted. To persist data, you can use a Docker volume to store the PostgreSQL data directory outside of the container.
Here’s an example of how to run the container with a persistent volume:
docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 -v postgres-data:/var/lib/postgresql/data postgres
In this command, we’ve added the -v
flag to mount a volume at /var/lib/postgresql/data
. This will store all database data in a persistent location on our host machine.
Connecting from an External Application
If you want to connect to your PostgreSQL database from an external application (such as a web server or another Docker container), you can use the same connection details as before:
Host: localhost
Port: 5432
Database: mydatabase
User: postgres
Password: mysecretpassword
Make sure to replace mydatabase
and mysecretpassword
with your own values.
Conclusion
In this tutorial, we’ve covered how to run a PostgreSQL database in a Docker container and connect to it from outside the container. We’ve also explored how to persist data using a Docker volume and connect to the database from an external application. With these skills, you should be able to set up your own PostgreSQL database in a Docker container and start using it for development or production purposes.