gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:05:08 30 4
gpt4 key购买 nike

我正在尝试将现有数据库导入 postgres docker 容器。
这就是我的处理方式:

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



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

但是当我执行命令时 \dt我有这个错误 Did not find any relations.
知道我的数据库已经有表了。
所以我做错了吗?

最佳答案

首先最好采用@LinPy 提到的方法。

或者最好在构建时复制。

文件

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

另一种选择是,您只需要为数据库创建编写脚本。
FROM postgres
ENV POSTGRES_DB=mydb

将为您创建数据库。

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 entrypoint将处理 SQL 脚本。

第二件事, 数据库名称的当前问题 , Postgress 不会简单地以大写形式对待它们,除非你做了一些 trick .

Postgresql treats the db name as lowercase, normalising. However, the field in the postgresapi does not replicate this behaviour, thus allowing you to create a database with Capital letters. 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.



postgres-api

将您的命令更改为
创建数据库
docker exec -it pg-docker bash
psql -U postgres
postgres=# CREATE DATABASE myDB;

验证数据库
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 +

所以导入命令将是
psql -U postgres mydb < BCS/mydb.sql

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

关于postgresql - 在 docker postgres 容器中导入 postgres 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58540320/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com