【发布时间】:2020-05-05 15:55:18
【问题描述】:
我无法通过 PDO 将我的 php 应用程序连接到 postgresql。我有经典错误:
SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
但是我可以使用 DBeaver 连接到我的数据库,例如通过我用于 PDO 的相同配置(相同的主机、相同的端口、相同的用户和相同的密码!),我可以看到 postgresql 与 netstat 命令的连接。
我通过 docker-compose 使用 docker。这是我的 docker-compose.yml :
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
depends_on:
- php
volumes:
- "${WWW_DIR}:/usr/share/nginx/html:ro"
- "${NGINX_LOG}:/var/log/nginx"
- "${NGINX_DIR}/nginx.conf:/etc/nginx/nginx.conf:ro"
ports:
- "127.0.0.1:8000:80"
php:
build: ${PHP_DIR}
container_name: php
depends_on:
- pgsql
- composer
volumes:
- "${PHP_LOG}/access.log:/var/log/access.log"
- "${PHP_LOG}/error.log:/var/log/error.log"
- "${WWW_DIR}:/var/www/html"
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
composer:
restart: 'no'
image: composer
command: install
volumes:
- "${WWW_DIR}:/app"
pgsql:
restart: always
build: ${PGSQL_DIR}
container_name: pgsql
volumes:
- "${PGSQL_DIR}/db:/var/lib/postgresql/data"
- "${PGSQL_LOG}:${POSTGRES_INITDB_WALDIR}:z"
ports:
- "127.0.0.1:5432:5432"
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_INITDB_WALDIR=${POSTGRES_INITDB_WALDIR}
这是我的 .env 文件:
# DIRs
WWW_DIR=../html
PHP_DIR=./php
NGINX_DIR=./nginx
PGSQL_DIR=./pgsql
# LOGs
NGINX_LOG=./nginx/log
PHP_LOG=./php/log
PGSQL_LOG=./pgsql/log
POSTGRES_INITDB_WALDIR=/var/log/pgsql
# DB
POSTGRES_USER=site
POSTGRES_PASSWORD=etis
POSTGRES_DB=dbname
这是我用于 postgresql 的 Dockerfile:
FROM postgres:alpine
# DB import
COPY db.sql /docker-entrypoint-initdb.d/
所以我错了吗?
提前致谢。
【问题讨论】:
标签: php postgresql pdo docker-compose