gpt4 book ai didi

postgresql - 对我的 postgresql 容器 docker 的远程控制有效但看不到创建的表

转载 作者:行者123 更新时间:2023-11-29 12:01:33 27 4
gpt4 key购买 nike

我是 docker 的新手,我创建了一个 dockerfile 来启动 postgresql 图像并在新数据库中插入数据。

当我运行 docker 时,它在容器中运行良好,我可以看到创建的数据库和表。

但是当我尝试从客户端 postgres 连接时,我看到创建了数据库但没有创建表,我们不知道为什么我和我的团队..

我的 DockerFile:

# Dockerfile
FROM postgres:9.6.5
RUN localedef -i fr_FR -c -f UTF-8 -A /usr/share/locale/locale.alias fr_FR.UTF-8
ENV LANG fr_FR.utf8
COPY postgresql.conf /
COPY sql/structure.sql /docker-entrypoint-initdb.d/

我的文件SQL结构.sql:

CREATE DATABASE DB_ALLOT_BOUCHON_NEW;
GRANT ALL PRIVILEGES ON DATABASE DB_ALLOT_BOUCHON_NEW TO postgres;
CREATE Table IF NOT EXISTS public.SUBSCR (
ID BIGINT NOT NULL,
subscriber_name VARCHAR(100) NOT NULL,
sp_array VARCHAR(100) NOT NULL,
PRIMARY KEY (ID)
);

INSERT INTO SUBSCR (ID, subscriber_name, sp_array) VALUES (1, 'client_1', 'sp_array client_1 client_2');
INSERT INTO SUBSCR (ID, subscriber_name, sp_array) VALUES (2, 'client_2', 'sp_array client_2 client_2');

构建和运行我的 dockerfile 的命令:

$ docker build -t allot_psql .
$ docker run -d --name custom_psql_running -p 5467:5432 allot_psql -c config_file=postgresql.conf
$ docker logs custom_psql_running
$ docker run -it --rm --link custom_psql_running postgres psql -h custom_psql_running -U postgres
# postgres=# SELECT * from subscr;

我容器中的结果:


postgres=# select * from subscr;
id | subscriber_name | sp_array
----+-----------------+----------------------------
1 | client_1 | sp_array client_1 client_2
2 | client_2 | sp_array client_2 client_2
(2 rows)

postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | subscr | table | postgres
(1 row)

postgres=# \d*
Invalid command \d*. Try \? for help.
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | subscr | table | postgres
(1 row)

postgres=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+--------+-------+----------+------------+-------------
public | subscr | table | postgres | 8192 bytes |
(1 row)

postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | subscr | table | postgres
(1 row)

postgres=# \
Invalid command \. Try \? for help.
postgres=# \q

一切似乎都运行良好,但是当我使用 postgresql 客户端时,我只能看到创建的数据库,但看不到里面的表。

jdbc:postgresql://localhost:5467/db_allot_bouchon_new
username:postgres

最佳答案

structure.sql 脚本在默认数据库 postgres 中创建表,而不是在 DB_ALLOT_BOUCHON_NEW 中。初始化容器后,您将连接到 postgres。要在创建后立即连接到 db_allot_bouchon_new

CREATE DATABASE DB_ALLOT_BOUCHON_NEW;
\connect db_allot_bouchon_new
...

This也可能有帮助。

关于postgresql - 对我的 postgresql 容器 docker 的远程控制有效但看不到创建的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57703068/

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