4

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.

3
  • you can share the log for postgres "docker logs container" Commented Aug 31, 2021 at 20:07
  • 1
    Have you told docker-compose to rebuild the container at some point since adding the postgres extension to the dockerfile? eg: docker-compose build or docker-compose up --build? Commented Aug 31, 2021 at 20:16
  • @Sammitch This is what I just did by finding a video on youTube. By adding libpq-dev and rebuilding the dockerfile is what did the trick and worked. Thanks for the answers, I appreciate your time. Commented Aug 31, 2021 at 20:28

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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 \ 
  ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.