How to persist data using postgres docker image? How to persist data using postgres docker image? docker docker

How to persist data using postgres docker image?


First, those environment variables look suspicious. Take a look at the documentation for the official Docker image, and note that you need POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD, rather than DB_NAME, DB_USER, and DB_PASS.

Other than that, you seem to mostly be on the right track. Here's a complete example:

First, I start a Postgres container. I'm locating the persistent storage somewhere outside of my home directory, because as you have already noted that files won't be owned by you, which can be confusing in your home directory (though not necessarily problematic):

docker run --rm --name postgres \  -v /tmp/postgres:/var/lib/postgresql/data \  -e POSTGRES_DB=larstest \  -e POSTGRES_USER=lars \  -e POSTGRES_PASSWORD=secret postgres

Since this is the first time I've started postgres pointing at that data directory, we will see it initialize the database:

The database cluster will be initialized with locale "en_US.utf8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /var/lib/postgresql/data ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okSuccess. You can now start the database server using:

Now, from another window, I can connect to it...

$ psql -h 172.17.0.4 -U lars larstestPassword for user lars: psql (9.5.4, server 9.6.0)WARNING: psql major version 9.5, server major version 9.6.         Some psql features might not work.Type "help" for help.

...and create some data:

larstest=# create table testtable (id integer);CREATE TABLElarstest=# insert into testtable values (1);INSERT 0 1larstest=# select * from testtable; id ----  1(1 row)

Now, I exit the container:

^CLOG:  received fast shutdown requestLOG:  aborting any active transactionsFATAL:  terminating connection due to administrator commandLOG:  autovacuum launcher shutting downLOG:  shutting downLOG:  database system is shut down

We can verify that it's no longer running:

$ docker ps | grep postgres

But if we start it up again with the same command line arguments;

docker run --rm --name postgres \  -v /tmp/postgres:/var/lib/postgresql/data \  -e POSTGRES_DB=larstest \  -e POSTGRES_USER=lars \  -e POSTGRES_PASSWORD=secret postgres

We see that it doesn't initialize the database, since it already exists, and skip straight to:

LOG:  database system was shut down at 2016-10-21 03:13:50 UTCLOG:  MultiXact member wraparound protections are now enabledLOG:  database system is ready to accept connectionsLOG:  autovacuum launcher started

At this point, we can re-connect to the database and find that our data still exists:

$ psql -h 172.17.0.2 -U lars larstestPassword for user lars: psql (9.5.4, server 9.6.0)WARNING: psql major version 9.5, server major version 9.6.         Some psql features might not work.Type "help" for help.larstest=# select * from testtable; id ----  1(1 row)

That's pretty much all there is to it.