When using Docker, you can use volumes to persist data even when you stop or restart the containers. We’ll create and use Docker volumes for PostgreSQL.
Prerequisites
To follow along with this tutorial:
- You should have Docker installed on your machine
- You should be comfortable with Docker commands and PostgreSQL
Step 1: Pull the PostgreSQL Image
First, we pull the PostgreSQL image from DockerHub:
Step 2: Create a Docker Volume
Next, let’s create a Docker volume to store the data. This volume will persist the data even if the container is removed.
$ docker volume create pg_data
Step 3: Run the PostgreSQL Container
Now that we’ve pulled the image and created a volume, we can run the PostgreSQL container attaching the created volume to it.
$ docker run -d \
--name my_postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-v pg_data:/var/lib/postgresql/data \
-p 5432:5432 \
postgres
This command runs my_postgres
in detached mode. Using –v pg_data:/var/lib/postgresql/data mounts the pg_data
volume to /var/lib/postgresql/data in the container. And using -p 5432:5432 maps port 5432 of my_postgres
to port 5432 on the host machine.
Step 4: Verify the Volume Usage
Now that we’ve created the volume, we can verify it’s being used. You can inspect the volume and check the contents.
$ docker volume inspect pgdata
Running this command will show details about the volume, including its mount point on your host system. You can navigate to this directory and see the PostgreSQL data files.
[
{
"CreatedAt": "2024-08-07T15:53:23+05:30",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/pg_data/_data",
"Name": "pg_data",
"Options": null,
"Scope": "local"
}
]
Step 5: Create a Database and Table
Connect to the Postgres instance and create a database and table.
Start a psql session:
$ docker exec -it my_postgres psql -U postgres
Create a new database:
Connect to the new database:
Create a table and insert some data:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
INSERT INTO users (name, email) VALUES ('Abby', 'abby@example.com'), ('Bob', 'bob@example.com');
Run a sample query:
Output:
id | name | email
----+------+------------------
1 | Abby | abby@example.com
2 | Bob | bob@example.com
Step 6: Stop and Remove the Container
Stop the running container and remove it. We do this so we can test that the data persists even if the container is stopped.
$ docker stop my_postgres
$ docker rm my_postgres
Step 7: Re-run the Postgres Container with the Same Volume
Start a new PostgreSQL container with the same volume to ensure data persistence.
$ docker run -d \
--name my_postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-v pgdata:/var/lib/postgresql/data \
-p 5432:5432 \
postgres
Connect to the Postgres instance and verify that the data persists.
Open a psql session:
$ docker exec -it my_postgres psql -U postgres
Connect to the mydb
database:
Verify the data in the users
table:
You should still see the output:
id | name | email
----+------+------------------
1 | Abby | abby@example.com
2 | Bob | bob@example.com
I hope this tutorial helps you understand how to use volumes to persists data when working with Docker.
Additional Resources
To learn more, read the following resources:
Happy exploring!
Bala Priya C is a developer and technical writer from India. She likes working at the intersection of math, programming, data science, and content creation. Her areas of interest and expertise include DevOps, data science, and natural language processing. She enjoys reading, writing, coding, and coffee! Currently, she’s working on learning and sharing her knowledge with the developer community by authoring tutorials, how-to guides, opinion pieces, and more. Bala also creates engaging resource overviews and coding tutorials.