如何对现有数据使用PostgreSQL容器? [英] How to use a PostgreSQL container with existing data?

查看:70
本文介绍了如何对现有数据使用PostgreSQL容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置PostgreSQL容器( https://hub.docker.com/_ / postgres / )。我有一些来自当前PostgreSQL实例的数据。我从 / var / lib / postgresql / data 复制了它,并想将其设置为PostgreSQL容器的卷。

I am trying to set up a PostgreSQL container (https://hub.docker.com/_/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
version: '2'

services:
  postgres9:
    image: postgres:9.4
    expose:
      - 5432
    volumes:
      - data:/var/lib/postgresql/data

volumes:
  data: {}





启动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容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆