AC-L3S
AC-L3S

Reputation: 91

Docker Laravel PostgreSQL - QueryException: could not find driver

I've been trying to run my new REST api calls but i get this error on postman:

Illuminate\Database\QueryException: could not find driver (SQL: insert into "companies" ("name", "establishment", "updated_at", "created_at") values (Tekmon, 2021-08-10, 2021-08-31 19:29:31, 2021-08-31 19:29:31) returning "id") in file /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671

I have succesfully run my migrations to create the database tables.

phpinfo on docker container does not have pdo_pgsql extension on the list.

Dockerfile:

FROM php:8.0.7-fpm

COPY composer.lock composer.json /var/www/

#Set working directory
WORKDIR /var/www

#Install dependecies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    libzip-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    postgresql-dev

#Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

#Install Extensions
RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql
RUN docker-php-ext-install pdo pdo_pgsql pgsql zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd

#Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

#Add user for laravel app
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

#Copy existing application directory contents
COPY . /var/www

#Copy existing application directory permissions
COPY --chown=www:www . /var/www/

#Change current user to www
USER www

#Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

Docker-compose file:

version: '3'
services:
    #POSTGRES Service
    db:
        image: postgres
        container_name: db
        restart: unless-stopped
        tty: true
        ports: 
            - "5432:5432"
        environment:
            POSTGRES_DB: tekmondb
            POSTGRES_PASSWORD: tekmonpass
            POSTGRES_USER: tekmon
        volumes: 
            - dbdata:/var/lib/postgresql/data
        networks: 
            - app-network
    #PHP Service
    app:
        build:
            context: .
            dockerfile: Dockerfile
        image: digitalocean.com/php
        container_name: app
        restart: unless-stopped
        tty: true
        environment: 
            SERVICE_NAME: app
            SERVICE_TAGS: dev
        working_dir: /var/www
        volumes:
            - ./:/var/www
            - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
        networks: 
            - app-network
    
    #Nginx Service
    webserver:
        image: nginx:alpine
        container_name: webserver
        restart: unless-stopped
        tty: true
        ports: 
            - "80:80"
            - "443:443"
        volumes: 
            - ./:/var/www
            - ./nginx/conf.d/:/etc/nginx/conf.d/
        networks:
            - app-network
    

#Docker networks
networks: 
    app-network:
        driver: bridge

#Volumes
volumes: 
    dbdata:
        driver: local     

Laravel db environment: DB_CONNECTION=pgsql

config/database.php: 'default' => env('DB_CONNECTION', 'pgsql'),

I've searched many questions for some hours now but nothing worked.

Thanks.

Upvotes: 4

Views: 5286

Answers (3)

pableiros
pableiros

Reputation: 16080

This worked for me using an apache-bullseye image:

FROM php:8.2.23-apache-bullseye

RUN set -eux; \
  apt-get update; \
  apt-get install -y --no-install-recommends \
# Adding this:
    libghc-postgresql-libpq-dev \ 
  ;

Upvotes: 1

This one worked for me

 FROM php:8.2-fpm-alpine

 RUN set -ex \
 && apk --no-cache add \
 postgresql-dev

 RUN docker-php-ext-install pdo pdo_pgsql

Reference: https://github.com/docker-library/php/issues/221

Upvotes: 1

AC-L3S
AC-L3S

Reputation: 91

As Sammitch below mentioned, and a video on YouTube by BPKodes, I needed to rebuild the container, which I didn't in the beginning, with libpq-dev instead of postgresql-dev. Hope this helps anyone.

Upvotes: 5

Related Questions