![](/img/trans.png)
[英]Error in Postgresql: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "TheGecko"
[英]Django + Docker: connection to server at "localhost" (127.0.0.1), port 5432 failed
我正在嘗試在 docker 中運行我的 Django 應用程序(Nginx,Gunicorn)。
但是對於請求http://167.99.137.32/admin/我有錯誤:(完整日志https://pastebin.com/0f8CqCQM )
onnection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Address not available
Is the server running on that host and accepting TCP/IP connections?
我正在嘗試來自Can't run the server on Django (connection denied) 的答案,但沒有解決我的問題
設置.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lk_potok_2',
'USER': 'postgres',
'PASSWORD': 'post222',
'HOST': 'localhost',
'PORT': 5432,
},
docker-compose.yml
version: '3.9'
services:
django:
build: . # path to Dockerfile
command: sh -c "gunicorn --bind 0.0.0.0:8000 potok.wsgi:application"
volumes:
- .:/project
- static:/project/static
expose:
- 8000
environment:
- DATABASE_URL=postgres://postgres:post222@localhost:5432/lk_potok_2"
- DEBUG=1
db:
image: postgres:13-alpine
volumes:
- pg_data:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=post222
- POSTGRES_DB=lk_potok_2
nginx:
image: nginx:1.19.8-alpine
depends_on:
- django
ports:
- "80:80"
volumes:
- static:/var/www/html/static
- ./nginx-conf.d/:/etc/nginx/conf.d
volumes:
pg_data:
static:
nginx-conf.nginx
upstream app {
server django:8000;
}
server {
listen 80;
server_name 167.99.137.32;
location / {
proxy_pass http://django:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /var/www/html/static/;
}
}
我正在嘗試sudo systemctl start postgresql和sudo systemctl enable postgresql (同樣的錯誤)
postgres 數據庫不再在localhost
運行。 在您的情況下(因為您將容器命名為db
)它是db
。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lk_potok_2',
'USER': 'postgres',
'PASSWORD': 'post222',
'HOST': 'db',
'PORT': 5432,
},
我真的不明白你為什么要在這里添加這個:
environment:
- DATABASE_URL=postgres://postgres:post222@localhost:5432/lk_potok_2"
因為您沒有在settings.py
中使用它。 但在這里它也必須是db
而不是localhost
。
- 編輯 -
關於為什么docker
可以識別其他容器的解釋可以在這里找到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.