Docker Community Forums

Share and learn in the Docker community.

PHP postgresql in docker compose

Hello,

I take the liberty of writing to you because I am blocked on a point that you will undoubtedly find basic.

I am unable to activate the php pgsql extension.

I am on an Unbuntu server and here is my docker compose.

Blockquote
version: ‘2’
services:
web:
image: lavoweb/php-7.4
ports:
- “80:80”
volumes:
- /var/www:/var/www/html
- /var/docker/php.ini-development:/usr/local/etc/php/php.ini-development
- /var/docker/php.ini-production:/usr/local/etc/php/php.ini-production
links:
- db:db
db:
image: postgres
restart: always
volumes:
- /mnt/postgresql:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: Alcapone!257
PGDATA: /var/lib/postgresql/data/pgdata
adminer:
image: adminer
restart: always
ports:
- 8080:8080

I did add “extension = pgsql.so” in the php.ini, but the “extension_loaded (‘pgsql’)” function tells me that it is not activated.

I don’t know how to install it with my php image.

cordially.

Hi,

resolve with this :

I add this code in image build of php :

RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pgsql pdo_pgsql

Your application runs inside a container and when it tries to connect to 127.0.0.1 then it expects the database server to run in the same container. And that is not the case. Your database runs in the container/service pgsql.

When you run your application on the host it works because everything runs on the same machine. Also your database client works because you map the same port on the host.

SOLUTION:

Change the reference to the database server in your application to point to pgsql instead of 127.0.0.1. To have the same working on host as well you can add the mapping to /etc/hosts as well (or the equivalent for Windows). Since you use an environment file, putting the database URL in there is even a better solution. You can do that once you have it working with the hardcoded value.