How to Connect PostgreSQL with Laravel using Docker

  1. Create Docker-composer.yml in root

version: “3.7”

networks:
    app-network:
        driver: bridge
services:
    app:
        build:
            context: ./
            dockerfile: Dockerfile
        image: laravel5-php-fpm-73
        container_name: app
        restart: unless-stopped
        tty: true
        working_dir: /var/www
        env_file:
            – src/.env
        volumes:
            – ./:/var/www
        networks:
            – app-network
    postgresql_database:
        image: postgres:latest
        container_name: postgresql
        restart: always
        tty: true
        environment:
            – POSTGRES_HOST_AUTH_METHOD=trust
            – POSTGRES_USER=postgres
            – POSTGRES_PASSWORD=postgres
            – PGDATA=/var/lib/postgresql/data
            – POSTGRES_DB=einqcbpdb
        hostname: postgres
        ports:
            – “5433:5432”
        volumes:
             – database-data:/var/lib/postgresql/data/
        networks:
            – app-network
 
    nginx:
        image: nginx:1.19.8-alpine
        container_name: nginx
        restart: unless-stopped
        tty: true
        ports:
            – “8100:80”
        volumes:
            – ./:/var/www
            – ./nginx/conf:/etc/nginx/conf.d
        networks:
            – app-network
    pgadmin:
        image: dpage/pgadmin4
        container_name: pgadmin4_container
        restart: always
        ports:
            – “5050:80”
        environment:
            PGADMIN_DEFAULT_EMAIL: pgadmin@test.com
            PGADMIN_DEFAULT_PASSWORD: admin
        volumes:
            – ‘./pgadmin:/var/lib/pgadmin’
volumes:
  database-data:
  pgadmin:

2) Add PHP image inside docker file

FROM php:7.3.33-fpm-buster

RUN docker-php-ext-install bcmath pdo_mysql

RUN apt-get update

RUN apt-get install -y git zip unzip

RUN apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pdo_pgsql

COPY src/ /var/www/

COPY –from=composer:latest /usr/bin/composer /usr/bin/composer

EXPOSE 9000

3) Add DB Credentials in .env file

DB_CONNECTION=pgsql

DB_HOST=host.docker.internal

DB_PORT=5433

DB_DATABASE=databasename

DB_USERNAME=postgres

DB_PASSWORD=postgres

PGSQL_CONTAINER_PORT=5433

4) Create Server and DB in PgAdmin 4

5) Find file postgresql.conf in C:\Program Files\PostgreSQL\14\data then set password_encryption = md5

6) Find file pg_hba.conf in C:\Program Files\PostgreSQL\13\data then change all METHOD to md5

7) Open command line (cmd,cmder,git bash…) and run psql -U postgres then enter your password when installed postgres sql

8) Restart service postgresql-x64-13 in your Service.

9) Add or uncomment PDO extension to php.ini

10) extension=pdo_pgsql   extension=pgsql

11) Run : docker-compose up

Leave a Reply

Your email address will not be published. Required fields are marked *