Â
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.