postgresql Docker-Compose Postgres 5432 绑定:地址已在使用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47069540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker-Compose Postgres 5432 Bind: Address already in use error
提问by BryceHayden
Alright so I've been searching for 3-4 days now and I can't seem to find a solution to my error. I have found similar errors, and some that seemed to be describing my exact issue but none of the solutions worked.
好的,所以我已经搜索了 3-4 天,但似乎找不到解决我的错误的方法。我发现了类似的错误,有些错误似乎描述了我的确切问题,但没有一个解决方案有效。
I'm trying to get a simply postgres, express, node app up and running. I want to be able to run docker-compose up -d
and have it build all of the images, volumes, etc. Then I will run a bash script that will seed my postgres db with data. However, I keep getting an error with the ports I'm using. I've removed all of my images, containers, and even reinstalled docker but I can't figure it out. I removed everything from my docker-compose except for the postgres and it still doesn't work.
我正在尝试启动并运行一个简单的 postgres、express、node 应用程序。我希望能够运行docker-compose up -d
并让它构建所有图像、卷等。然后我将运行一个 bash 脚本,该脚本将为我的 postgres 数据库提供数据。但是,我使用的端口一直出错。我已经删除了我的所有图像、容器,甚至重新安装了 docker,但我无法弄清楚。我从 docker-compose 中删除了除 postgres 之外的所有内容,但它仍然无法正常工作。
version: '3'
services:
postgres:
image: postgres:10.0
volumes:
- /var/lib/postgresql/data
ports:
- '5432:5432'
Then on my host machine I simply plan on running the following bash script.
然后在我的主机上,我只是计划运行以下 bash 脚本。
#!/bin/bash
host="postgres.dev"
adminUser="postgres"
psql -h $host -U $adminUser -c "select 1 from pg_database where datname = 'table_name';" | grep -q 1 || psql -h $host -U $adminUser -f ./"$(dirname ERROR: for pg-db Cannot start service postgres: driver failed programming external connectivity on endpoint pg-db (b3e5697cd563264250479682ec83d4a232d0d4bd679a787ad2089e944dda9e2f): Error starting userland proxy: listen tcp 0.0.0Creating test-api ... done
ERROR: for postgres Cannot start service postgres: driver failed programming external connectivity on endpoint pg-db (b3e5697cd563264250479682ec83d4a232d0d4bd679a787ad2089e944dda9e2f): Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
ERROR: Encountered errors while bringing up the project.
)"/init-local-db.sql
I know that this approach should work since I'm patterning it after a work project...which works for them. But here's the error I get:
我知道这种方法应该有效,因为我是在工作项目之后对其进行模式化的……这对他们有用。但这是我得到的错误:
sudo service postgresql stop
sudo update-rc.d postgresql disable
I know some people say to simply change the port number so '5433:5432'
but my problem with this is that when you install postgres by default its port is 5432, and I know it's not necessary to change it(because the work project doesn't). Any thoughts?
我知道有些人说只需更改端口号,'5433:5432'
但我的问题是当您默认安装 postgres 时,它的端口是 5432,我知道没有必要更改它(因为工作项目没有)。有什么想法吗?
Update(next morning):
更新(第二天早上):
Okay I still have no idea why the error popped up in the first place, as I used lsof -i tcp:5432 (along with netstat) and nothing came up as using that port. I put my computer in suspend mode and went to bed. In the morning I woke up, changed my postgres version to 9.6 to see if that was it, and everything worked. I then switched it back to postgres 10.0 and again everything worked. Hopefully it won't pop back up again, but I have no idea why it popped up in the first place.
好吧,我仍然不知道为什么首先会出现错误,因为我使用了 lsof -i tcp:5432(以及 netstat)并且没有出现使用该端口的情况。我将计算机置于挂起模式并上床睡觉。早上醒来,将我的 postgres 版本更改为 9.6,看看是不是这样,一切正常。然后我将其切换回 postgres 10.0,一切正常。希望它不会再次弹出,但我不知道为什么它首先弹出。
回答by Sergei Krivosheenko
There is only one reason you may be getting this error. You have PostgreSQL installed on your local machine and it's running, occupying the port 5432.
您可能会收到此错误的原因只有一个。您在本地机器上安装了 PostgreSQL 并且它正在运行,占用端口 5432。
You have the following options:
您有以下选择:
Disable (and remove from startup) PostgreSQL on your local machine. - Your Docker Compose will run.
sudo service postgresql stop sudo update-rc.d postgresql disable
Use a different port in docker-compose. There is nothing wrong with applying '5433:5432'. Other services of your docker-compose will connect to postgres by 5432 port. From your local machine you'll be able you address postgres by localhost:5433.
在本地机器上禁用(并从启动中删除)PostgreSQL。- 您的 Docker Compose 将运行。
##代码##在 docker-compose 中使用不同的端口。应用'5433:5432'没有任何问题。docker-compose 的其他服务将通过 5432 端口连接到 postgres。在您的本地机器上,您将能够通过 localhost:5433 访问 postgres。