Call to undefined function pg_connect()

Hello, I’m new to docker and am trying to set up a website with pgsql.

Things launch fine, and I even have a table imported to the pgsql container. The configs for this project is thus:


version: '3'
        image: nginx:1.18.0
            - "80:80"
            - ./nginx.conf:/etc/nginx/conf.d/nginx.conf
            - ./app:/app
            context: .
        container_name: php
            - ./app:/app
        image: postgres
            POSTGRES_USER: 'dash'
            POSTGRES_PASSWORD: 'password'
            POSTGRES_DB: 'dash'
            - pgsql-data:/var/lib/postgresql
            - ./app:/app
            - 3306:3306
    pgsql-data: {}


FROM php:7.0-fpm
RUN apt-get install -y libpq-dev \
  && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
  && docker-php-ext-install pgsql pdo pdo_pgsql 

And I have a very simple index.php set up:

$connection = pg_connect("host=composedash-pgsql-1 port=3306 dbname=dash user=dash password=password");
if($connection) {
        echo 'connected';
} else {
        echo 'there has been an error connecting';

But, of course, I get the error:

Fatal error : Uncaught Error: Call to undefined function pg_connect() in /app/public/index.php:2 Stack trace: #0 {main} thrown in /app/public/index.php on line 2

Can anyone help me figure out what I’m doing wrong?

Is that the whole Dockerfile? PHP 7.0-fpm is old. apt-get update would be required to install packages but that can’t run because of missig release file error.

I also noticed that there is only one dash in -with-pgsql. It should be --with-pgsql

Can you build the image using same Dockerfile but with newer PHP versions? 8.1 for example.

Yes, it’s the entire docker file. I have changed it to simply php:fpm, does that work? I honestly just assume that is the latest version.
It seems like it is?

When I found tutorials for setting up a web container, this was all the Dockerfile had in it. I’m not quite sure what else would go in there.

Thank you for the help. I have applied the fix with -- as well as use php:fpm, but it still reports the same issue.

Can you share the tutorial link?

Unless someone overwritten the original php image tag, without apt-get update apt can’t install anything since it needs the cache to know about packages. So when you ran the docker build command it should have been failed. How did you build the image exactly?

The original tutorial was for MySQL, so I’ve just gone and replaced the .yaml stuff from MySQL to Postgres. It’s been a few months since I originally set it up, and put it down when I initially ran into DB issues trying to use mysql. I don’t remember what those errors were, unfortunately.

If you skip down to where they actually do the “Getting Started,” that should be how I built things.

At some point down the line, with a lot of messing around, I got to the current Dockerfile from some 2016 github issue thread, hah.

There was no apt-get install command in the tutorial. That could work, but I still don’t think that yours could. Are you sure that the docker build ran? It is possible that you had a Dockerfile once, built the image, ran the compose project, changed the Dockerfile, but didn’t use the --build option passed to docker compose up so you still used the original image which didn’t have postgres in it. That would explain why you didn’t see any error. For fixing the apt repository, you can do this:

FROM php:7.0-fpm

RUN sed -i -e 's/' \
           -e 's|||g' \
           -e '/stretch-updates/d' /etc/apt/sources.list

You should also fix the confiure option and use apt-get update before apt-get install

FROM php:7.0-fpm

RUN sed -i -e 's/' \
           -e 's|||g' \
           -e '/stretch-updates/d' /etc/apt/sources.list

RUN apt-get update \
  && apt-get install -y libpq-dev \
  && docker-php-ext-configure pgsql --with-pgsql=/usr/local/pgsql \
  && docker-php-ext-install pgsql pdo pdo_pgsql 

And run the compose project this way:

docker compose up -d --build
1 Like

Thank you, I was made a bit cross-eyed by how different all the tutorials I found were, so I went with the one that seemed the most simple to start. I will try these out when next I get the chance and report back!

I would recommend to use a Docker network. (Doc)

Create one (in compose or CLI) and attach your web app and pgsql to it. Remove the ports section of pgsql to not expose the DB publicly.

Use the compose service name pgsql as hostname in your web app to connect via the internal Docker network.

You can leave the ports exposed if you need to connect to DB externally.

A Docker Network is created by default in compose (doc), so you should even be able to just use pgsql as hostname, without any other changes, but I find it a lot cleaner with an explicit Docker network.

Generally a good advice, but I don’t see how it is related to the issue. The Docker network is used in the postgres connection or at least tried to be used. Of course the service name instead of the container name would have been better.

Although it’s true that the port is wrong as well as the code was converted from a mysql example.

So @schnoodly when you are finished with fixing the missing function, you will need to fix the port. Always read the docker image’s description and the documentation of the application itself. Postgresql uses port 5432 by default so you will need to connect to that port, not 3306. If you forward ports from the host to connect to the DB from an IDE for example, you also need to change at least the container port in the port mapping to be 5432, but you probably want to change the host port as well. And if you don’t want to access it from outside the containers, do s @bluepuma77 suggested and remove the port mapping.

If you have problem with the ports when you finally have a working pg_connect function, I suggest opening another topic to discuss that.

1 Like

Thank you, pg_connect() seems to be working now! I went through and got rid of old images and just went to php 8.2, and for good measure re-downloaded all the other images.

I am running into issues with something else that I’m pretty sure is outside of this forum’s purview (a php call says the driver isn’t installed, but phpinfo() says it is – pretty sure it’s an issue with a js plugin I’m using called DataTables), but I’ll make a new thread if I find an issue relevant for here!