在 docker 中将 pgadmin 连接到 postgres
Connecting pgadmin to postgres in docker
我有一个 docker-compose
文件,其中包含 python
、nginx
、postgres
和 pgadmin
:
的服务
services:
postgres:
image: postgres:9.6
env_file: .env
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5431:5431"
pgadmin:
image: dpage/pgadmin4
links:
- postgres
depends_on:
- postgres
environment:
PGADMIN_DEFAULT_EMAIL: admin@admin.com
PGADMIN_DEFAULT_PASSWORD: pwdpwd
volumes:
- pgadmin:/root/.pgadmin
ports:
- "5050:80"
backend:
build:
context: ./foobar # This refs a Dockerfile with Python and Django requirements
command: ["/wait-for-it.sh", "postgres:5431", "--", "/gunicorn.sh"]
volumes:
- staticfiles_root:/foobar/static
depends_on:
- postgres
nginx:
build:
context: ./foobar/docker/nginx
volumes:
- staticfiles_root:/foobar/static
depends_on:
- backend
ports:
- "0.0.0.0:80:80"
volumes:
postgres_data:
staticfiles_root:
pgadmin:
当我运行docker-compose up
访问localhost:5050
时,我看到了pgadmin
界面。当我尝试在那里创建一个新服务器时,使用 localhost
或 0.0.0.0
作为主机名,5431
作为端口,我收到错误 "Could not connect to server"。如果我删除这些并在 "Service" 字段中输入 postgres
,我会收到错误 "definition of service "postgres" not found"。如何使用 pgadmin
连接到数据库?
请注意,默认的 postgres 端口是 5432 而不是 5431。您应该在撰写文件中更新 postgres 服务的端口映射。错误的端口可能是您报告问题的原因。更改端口映射,然后尝试连接到 postgres:5432。 localhost:5432 将不起作用。
当您 运行 docker 为文件夹名称添加前缀时,docker 容器名称会更改(以保持容器名称唯一)。您可以使用 container_name
属性
强制容器的名称
version: "3"
services:
# postgres database
postgres:
image: postgres:12.3
container_name: postgres
environment:
- POSTGRES_DB=admin
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=admin
- POSTGRES_HOST_AUTH_METHOD=trust # allow all connections without a password. This is *not* recommended for prod
volumes:
- database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
ports:
- "5432:5432"
# pgadmin for managing postgis db (runs at localhost:5050)
# To add the above postgres server to pgadmin, use hostname as defined by docker: 'postgres'
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin
environment:
- PGADMIN_DEFAULT_EMAIL=admin
- PGADMIN_DEFAULT_PASSWORD=admin
- PGADMIN_LISTEN_PORT=5050
ports:
- "5050:5050"
volumes:
database-data:
另一种选择是使用
将 postgres 容器连接到本地主机
network_mode: host
但是这样你就失去了与 docker 良好的网络隔离
我有一个 docker-compose
文件,其中包含 python
、nginx
、postgres
和 pgadmin
:
services:
postgres:
image: postgres:9.6
env_file: .env
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5431:5431"
pgadmin:
image: dpage/pgadmin4
links:
- postgres
depends_on:
- postgres
environment:
PGADMIN_DEFAULT_EMAIL: admin@admin.com
PGADMIN_DEFAULT_PASSWORD: pwdpwd
volumes:
- pgadmin:/root/.pgadmin
ports:
- "5050:80"
backend:
build:
context: ./foobar # This refs a Dockerfile with Python and Django requirements
command: ["/wait-for-it.sh", "postgres:5431", "--", "/gunicorn.sh"]
volumes:
- staticfiles_root:/foobar/static
depends_on:
- postgres
nginx:
build:
context: ./foobar/docker/nginx
volumes:
- staticfiles_root:/foobar/static
depends_on:
- backend
ports:
- "0.0.0.0:80:80"
volumes:
postgres_data:
staticfiles_root:
pgadmin:
当我运行docker-compose up
访问localhost:5050
时,我看到了pgadmin
界面。当我尝试在那里创建一个新服务器时,使用 localhost
或 0.0.0.0
作为主机名,5431
作为端口,我收到错误 "Could not connect to server"。如果我删除这些并在 "Service" 字段中输入 postgres
,我会收到错误 "definition of service "postgres" not found"。如何使用 pgadmin
连接到数据库?
请注意,默认的 postgres 端口是 5432 而不是 5431。您应该在撰写文件中更新 postgres 服务的端口映射。错误的端口可能是您报告问题的原因。更改端口映射,然后尝试连接到 postgres:5432。 localhost:5432 将不起作用。
当您 运行 docker 为文件夹名称添加前缀时,docker 容器名称会更改(以保持容器名称唯一)。您可以使用 container_name
属性
version: "3"
services:
# postgres database
postgres:
image: postgres:12.3
container_name: postgres
environment:
- POSTGRES_DB=admin
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=admin
- POSTGRES_HOST_AUTH_METHOD=trust # allow all connections without a password. This is *not* recommended for prod
volumes:
- database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
ports:
- "5432:5432"
# pgadmin for managing postgis db (runs at localhost:5050)
# To add the above postgres server to pgadmin, use hostname as defined by docker: 'postgres'
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin
environment:
- PGADMIN_DEFAULT_EMAIL=admin
- PGADMIN_DEFAULT_PASSWORD=admin
- PGADMIN_LISTEN_PORT=5050
ports:
- "5050:5050"
volumes:
database-data:
另一种选择是使用
将 postgres 容器连接到本地主机network_mode: host
但是这样你就失去了与 docker 良好的网络隔离