1

I am beginner docker user. I installed docker and postgresql in Mac OS. and why most of documents mention the directory /var/lib/postgresql/data as an volume setting??? Because in my local directory, there is not existed /var/lib/postgresql.. Is it default option? or am I missing something?

1
  • 1
    Please include the docs/instructions you are referring to in the question. Note that’s probably the path IN the container, not on the host Commented Feb 3, 2023 at 7:00

2 Answers 2

2

Yes, correct, /var/lib/postgresql does not exist on your local computer, but it does in the created container. The volumes parameter is used to associate the local data with the container data, in order to preserve the data in case the container crashes

For example:

volumes:
  - ./../database/main:/var/lib/postgresql/data

Above we link the local directory from the left side to the container directory

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. As you mentioned, if I want to preserve the data in local, I need to connect local directory with /var/lib/postgresql/data/ right?
@yeonsookkwak, If you really want to have the DB files on your local machine then yes, you need a bind mount type of volume. Please note that "Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts." (docs.docker.com/storage/volumes).
0

If you are using official PostgreSQL image from Docker Hub, then you can check the contents of its Dockerfile. E.g. here is a fragment of postgres:15 image responsible for data directory:

ENV PGDATA /var/lib/postgresql/data
# this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values)
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA"
VOLUME /var/lib/postgresql/data

As you can see Postgres is configured to have data in that directory. And to persist the data even if container is stopped and removed, the volume is created. Volumes have lifetime independent of the container which allows them to "survive".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.