docker network create pg
docker run -it --rm -P -p 5432:5432 --name pg -e POSTGRES_PASSWORD=password -e POSTGRES_USER=postgres -e POSTGRES_DB=production -v pgdata:/var/lib/postgresql/data --network=pg postgres
pgdata is the name of the docker volume. You can check it using docker volume ls
and docker volume inspect pgdata
.
You can delete the docker volume by docker volume rm pgdata
and prune all volumes using docker volume prune
docker volumes are created in your central docker directory (usually /var/lib/docker/overlay2
). You generally dont need to care about this.
You can now access it in many ways:
psql postgresql://postgres:password@localhost:5432/production
docker run -it --rm --network pg postgres psql -h pg -U postgres