我正在尝试设置PostgreSQL容器( hub.docker/_ / postgres / )。我有一些来自当前PostgreSQL实例的数据。我从 / var / lib / postgresql / data 复制了它,并想将其设置为PostgreSQL容器的卷。
I am trying to set up a PostgreSQL container (hub.docker/_/postgres/). I have some data from a current PostgreSQL instance. I copied it from /var/lib/postgresql/data and want to set it as a volume to a PostgreSQL container.
我从docker-compose.yml文件中获取有关PostgreSQL的部分:
My part from docker-compose.yml file about PostgreSQL:
db: image: postgres:9.4 ports: - 5432:5432 environment: POSTGRES_PASSWORD: postgres POSTGRES_USER: postgres PGDATA : /var/lib/postgresql/data volumes: - /projects/own/docker_php/pgdata:/var/lib/postgresql/data当我制作docker-compose时,我收到以下消息:
When I make docker-compose up I get this message:
db_1 | initdb: directory "/var/lib/postgresql/data" exists but is not empty db_1 | If you want to create a new database system, either remove or empty db_1 | the directory "/var/lib/postgresql/data" or run initdb db_1 | with an argument other than "/var/lib/postgresql/data".我试图从容器创建自己的映像,所以我的Dockerfile是:
I tried to create my own image from the container, so my Dockerfile is:
FROM postgres:9.4 COPY pgdata /var/lib/postgresql/data但是我遇到了同样的错误,我在做什么错?
But I got the same error, what am I doing wrong?
我使用pg_dumpall获得了SQL,并将其放在/docker-entrypoint-initdb.d中,但是每次我 docker-compose up 时都会执行此文件。
I got SQL using pg_dumpall and put it in /docker-entrypoint-initdb.d, but this file executes every time I do docker-compose up.
推荐答案要基于irakli的答案,请使用以下更新的解决方案:
To build on irakli's answer, here's an updated solution:
- 使用更新的版本2 Docker Compose文件
- 单独的卷部分
- 已删除其他设置
- use newer version 2 Docker Compose file
- separate volumes section
- extra settings deleted
启动Postgres数据库服务器:
demo
Start Postgres database server:
$ docker-compose up显示数据库中的所有表。在另一个终端中,与容器的Postgres对话:
Show all tables in the database. In another terminal, talk to the container's Postgres:
$ docker exec -it $(docker-compose ps -q postgres9 ) psql -Upostgres -c '\z'由于数据库为空白,因此不会显示任何内容。创建表:
It'll show nothing, as the database is blank. Create a table:
$ docker exec -it $(docker-compose ps -q postgres9 ) psql -Upostgres -c 'create table beer()'列出新创建的表:
$ docker exec -it $(docker-compose ps -q postgres9 ) psql -Upostgres -c '\z' Access privileges Schema | Name | Type | Access privileges | Column access privileges --------+-----------+-------+-------------------+-------------------------- public | beer | table | |是的!现在,我们使用共享存储卷启动了Postgres数据库,并在其中存储了一些数据。下一步是检查服务器停止后数据是否仍然存在。
Yay! We've now started a Postgres database using a shared storage volume, and stored some data in it. Next step is to check that the data actually sticks around after the server stops.
现在,杀死Postgres服务器容器:
Now, kill the Postgres server container:
$ docker-compose stop启动再次使用Postgres容器:
Start up the Postgres container again:
$ docker-compose up我们希望数据库服务器将重新使用存储,因此我们非常重要的数据仍然存在。检查:
We expect that the database server will re-use the storage, so our very important data is still there. Check:
$ docker exec -it $(docker-compose ps -q postgres9 ) psql -Upostgres -c '\z' Access privileges Schema | Name | Type | Access privileges | Column access privileges --------+-----------+-------+-------------------+-------------------------- public | beer | table | |我们已成功使用新型Docker Compose文件使用外部数据运行Postgres数据库
We've successfully used a new-style Docker Compose file to run a Postgres database using an external data volume, and checked that it keeps our data safe and sound.
首先,进行备份,将数据存储在主机上:
First, make a backup, storing our data on the host:
$ docker exec -it $(docker-compose ps -q postgres9 ) pg_dump -Upostgres > backup.sql从访客数据库中删除数据:
Zap our data from the guest database:
$ docker exec -it $(docker-compose ps -q postgres9 ) psql -Upostgres -c 'drop table beer'将备份(存储在主机上)恢复到Postgres容器中。
Restore our backup (stored on the host) into the Postgres container.
注意:使用 exec -i,不 -it,否则将出现输入设备不是TTY错误。
Note: use "exec -i", not "-it", otherwise you'll get a "input device is not a TTY" error.
$ docker exec -i $(docker-compose ps -q postgres9 ) psql -Upostgres < backup.sql列出表以验证还原是否有效:
List the tables to verify the restore worked:
$ docker exec -it $(docker-compose ps -q postgres9 ) psql -Upostgres -c '\z' Access privileges Schema | Name | Type | Access privileges | Column access privileges --------+-----------+-------+-------------------+-------------------------- public | beer | table | |总而言之,我们已验证可以启动数据库,并且数据在重新启动后仍然存在,我们可以从主机将备份还原到其中。
To sum up, we've verified that we can start a database, the data persists after a restart, and we can restore a backup into it from the host.
感谢Tomasz!
更多推荐
如何对现有数据使用PostgreSQL容器?
发布评论