Permanent Storage for Postgres#
You should have done the last lab twice. On the second try you should have noticed that the data from the first try was gone. The lesson: The writable layer of a container is not for storing your precious data! In this lab we’ll make a permanent home for your data using Docker volumes. There are many kinds of Docker volumes but for our purposes we’ll use a shared directory.
Create a Data Directory#
We need a database for our dev box. The purposed of the data on the dev box is to enable programmers to try out new features and test their code. When we go into production we’ll have to do some of these steps again on the production database. On your dev box create a directory in your home directory:
$ mkdir $HOME/data
Don’t create the data directory inside of your repo!
Create the Postgres Container#
Now let’s create a Postgres container again, this time with a volume mount:
$ docker run -d --rm \ -v $HOME/data:/var/lib/postgresql/data \ -e POSTGRES_PASSWORD=django -e POSTGRES_DB=mysite \ -p 5432:5432 \ postgres:14.1
Repeat the SQL commands from the Hello Postgres lab.
View the Data Files#
ls command to view the files in
$HOME/data. Do you notice anything? Who are the files owned by?
Stop and Restart the Postgres Container#
Now stop and restart the Postgres container. Connect to the database. Is your data still there?