简体   繁体   English

无法从我的本地机器连接到 docker 容器中的 postgres 数据库

[英]Can not connect to a postgres database in a docker container from my local machine

I have a strange problem with docker and postgres.我对 docker 和 postgres 有一个奇怪的问题。 I can not connect from my local machine to psql inside the docker container.我无法从本地机器连接到 docker 容器内的 psql。

First of all I created my container with this command:首先,我使用以下命令创建了我的容器:
docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres

After that I executed the container with following command:之后,我使用以下命令执行了容器:
docker exec -it my-postgres bash

Inside the container I started psql with the postgres user:在容器内,我使用 postgres 用户启动了 psql:
psql -U postgres
And I created a new database:我创建了一个新数据库:
CREATE DATABASE mytestdb;

Everything works inside the container, but if I want to connect to it from my machine it does only show the database from my machine.一切都在容器内工作,但如果我想从我的机器连接到它,它只会显示我机器上的数据库。 I tried this to connect to postgres:我试过这个连接到postgres:
psql -h localhost -p 5432 -U postgres

Which should me show something like this:我应该显示这样的内容:

                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 mytestdb  | 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          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(4 rows)

But instead I get this:但是我得到了这个:

                                          List of databases
      Name       |      Owner      | Encoding | Collate | Ctype   |        Access privileges          
-----------------+-----------------+----------+---------+---------+----------------------------------
 user            | user            | UTF8     | C       | C       | 
 postgres        | user            | UTF8     | C       | C       | 
 template0       | user            | UTF8     | C       | C       | =c/user                 +
                 |                 |          |         |         | user =CTc/user
 template1       | user            | UTF8     | C       | C       | =c/user                 +
                 |                 |          |         |         | user =CTc/user
(4 rows)

(END)

In every tutorial, I saw they did it, but for me, it doesn't work.在每个教程中,我都看到他们做到了,但对我来说,它不起作用。 So please help me.所以请帮助我。

The problem is when you creating the db.问题是当您创建数据库时。 CREATE DATABASE mytestdb you forgot to add ; CREATE DATABASE mytestdb你忘记添加了; the command should be like CREATE DATABASE mytestdb;命令应该类似于CREATE DATABASE mytestdb;

And then you have your db and you can access it from inside the container or your host with these commands然后你有你的数据库,你可以使用这些命令从容器或主机内部访问它

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

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