📅  最后修改于: 2023-12-03 15:18:38.246000             🧑  作者: Mango
In this tutorial, we'll go through the process of setting up a PostgreSQL docker container with a persistent volume using shell commands.
Before we begin, make sure that you have the following installed on your machine:
The first step is to create a named volume that will be used to store PostgreSQL data. Open your terminal and run the following command:
docker volume create pgdata
This will create a new Docker volume named pgdata
.
Now that we have created a volume, we can start a PostgreSQL container and configure it to use the volume for its data. Run the following command in your terminal:
docker run --name postgresql -e POSTGRES_PASSWORD=mysecretpassword -v pgdata:/var/lib/postgresql/data -d postgres:latest
This command will start a PostgreSQL container named postgresql
, set the password for the root user to mysecretpassword
, use the pgdata
volume we created earlier as the data directory, and run the container in detached mode.
To verify that the PostgreSQL container is running, run the following command in your terminal:
docker ps
This will show you all running containers, and you should see a container named postgresql
in the list.
To connect to the PostgreSQL server, you can use any PostgreSQL client that supports the PostgreSQL protocol. One popular option is the psql
command-line client.
To start a psql
session, run the following command in your terminal:
docker exec -it postgresql psql -U postgres
This will start a psql
session with the PostgreSQL server running inside the container, using the root user.
In this tutorial, we learned how to create a persistent volume using Docker, and use it to store data for a PostgreSQL container. We also saw how to start a container, configure it to use the volume, and connect to the PostgreSQL server using a client.