📜  postgresql docker volume - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:18:38.246000             🧑  作者: Mango

PostgreSQL Docker Volume - Shell-Bash

In this tutorial, we'll go through the process of setting up a PostgreSQL docker container with a persistent volume using shell commands.

Prerequisites

Before we begin, make sure that you have the following installed on your machine:

  • Docker
  • Shell or Bash terminal
Step 1: Create a volume

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.

Step 2: Start a PostgreSQL container

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.

Step 3: Verify container is running

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.

Step 4: Connect to PostgreSQL

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.

Conclusion

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.