简体   繁体   English

在 docker postgres 容器中导入 postgres 数据库

[英]Importing postgres database in a docker postgres container

I am trying to import an existing database into a postgres docker container.我正在尝试将现有数据库导入 postgres docker 容器。 This is how I proceed:这就是我进行的方式:

docker run --name pg-docker -e POSTGRES_PASSWORD=***** -d -p 5432:5432 -v BCS/postgres_data postgres docker 运行 --name pg-docker -e POSTGRES_PASSWORD=***** -d -p 5432:5432 -v BCS/postgres_data postgres

Then然后

docker exec -it pg-docker bash
psql -U postgres
postgres=# CREATE DATABASE myDB;
psql -U postgres myDB < BCS/mydb.sql

but when i execute the command \dt I have this error Did not find any relations.但是当我执行命令\dt我有这个错误Did not find any relations. knowing that my database has already tables.知道我的数据库已经有表。 So waht I am doing wrong?那么我做错了吗?

If you are sure that the database installed everything correctly, are you are still not seeing tables, there are 2 things you should double-check:如果您确定数据库正确安装了所有内容,您是否仍然看不到表,您应该仔细检查两件事:

  • When you connect, are you connecting to the right database?连接时,您是否连接到正确的数据库? If you are using psql in a terminal, the database is specified with the -d switch.如果您在终端中使用psql ,则使用-d开关指定数据库。
 psql -h <host> -U <user> -d <dbname>

You can also change your database after you connect using the \connect <dbname> command.您还可以在使用\connect <dbname>命令连接后更改数据库。

  • Are you specifying the right schema?您是否指定了正确的架构? \dt will show you tables, but you need to specify a schema first using set schema : \dt将向您显示表,但您需要先使用set schema指定架构:
postgres=# \dt

...
<no tables>
...

postgres=# set schema 'my_schema';
postgres=# \dt

...
<my tables>
...

First thing better to go with the approach that is mentioned by @LinPy.首先使用@LinPy 提到的方法对 go 更好。

Or better to copy at build time.或者更好地在构建时复制。

Dockerfile Dockerfile

FROM postgres
COPY mydb.sql /docker-entrypoint-initdb.d/

Another option, you do need to script for only DB creation.另一种选择是,您确实需要为仅创建数据库编写脚本。

FROM postgres
ENV POSTGRES_DB=mydb

The will create DB for you.将为您创建数据库。

POSTGRES_DB POSTGRES_DB

This optional environment variable can be used to define a different name for the default database that is created when the image is first started.此可选环境变量可用于为首次启动映像时创建的默认数据库定义不同的名称。 If it is not specified, then the value of POSTGRES_USER will be used.如果未指定,则将使用 POSTGRES_USER 的值。

In the above, the Postgres entrypoint will take care of the SQL script.在上面, Postgres 入口点将处理 SQL 脚本。

Second thing, the current issue with database name , Postgress will not treat them in uppercase simply, unless you did sometrick .第二件事,数据库名称的当前问题,Postgress 不会简单地用大写来处理它们,除非你做了一些技巧

Postgresql treats the db name as lowercase, normalising. Postgresql 将数据库名称视为小写,规范化。 However, the field in the postgresapi does not replicate this behaviour, thus allowing you to create a database with Capital letters.但是,postgresapi 中的字段不会复制此行为,因此允许您使用大写字母创建数据库。 The fix could be to warn the user that no uppercase letters are allowed in the db aname and to add in a validation rule to the API to stop a user creating such a database.修复可能是警告用户在 db aname 中不允许使用大写字母,并向 API 添加验证规则以阻止用户创建此类数据库。

postgres-api postgres api

Change your command to create DB更改您的命令以创建数据库

docker exec -it pg-docker bash
psql -U postgres
postgres=# CREATE DATABASE myDB;

verfiy DB验证数据库

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 mydb      | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

so the import command will be所以导入命令将是

psql -U postgres mydb < BCS/mydb.sql

or或者

psql  -d mydb -U postgres -f ab.sql

You can also have your sql scripts being redirected to the container like that:您还可以将 sql 脚本重定向到容器,如下所示:

Postgres user: Postgres 用户:

docker exec -i my-postgres-container psql -U postgres < created-db.sql

Regular user:普通用户:

docker exec -i my-postgres-container psql -d my-db -U my-user < create-schema.sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM