How to Connect PostgreSQL with Laravel using Docker
- Create Docker-composer.yml in root
version: “3.7”
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