Can't get postgres to work

I’m building my stack with nginx, php-fpm and postgres.
PHP is already working with nginx. But I can’t connect to postgres.

This is the (partial) code in my docker-compose file (version 2)

postgresql:
        image: postgres
        container_name: postgres-stubb
        ports:
            - "5431:5432"       
        environment:         
            - POSTGRES_DB=postgres
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=d4REn0LdCH4B
        volumes:
            - ./env-config/postgres.conf:/etc/postgresql.conf
            - /srv/docker/postgresql:/var/lib/postgresql    
        command: postgres -c config_file=/etc/postgresql.conf
        restart: always

Basically, I’m using the official image and mapping to 5432. At first, I tried 5432:5432 but then I changed to 5431:5432 just to try something different. Neither work.

My postgresql.conf file was taken from the postgres image and says:
listen_addresses = '*'

Through my other containers I’m running Laravel 5 with this config:
> DB_CONNECTION=pgsql

    DB_HOST=127.0.0.1
    DB_PORT=5431
    DB_DATABASE=postgres
    DB_USERNAME=postgres
    DB_PASSWORD=d4REn0LdCH4B

And the error is:

SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host “127.0.0.1” and accepting
TCP/IP connections on port 5431? (SQL: select * from “cards”)

I spent the whole day trying to solve this with no luck. Any clue?

Normally all the containers you want to talk to each other are included in the compose file and there is a private dns server on a private network connecting everything.

In that case your compose file should look something like this:

services:
  postgresql:
        image: postgres
        environment:         
            - POSTGRES_DB=postgres
   -----------
  laravel:

      --------

       environment:
            - DB_CONNECTION=pgsql
            - DB_HOST=postgresql
            - DB_PORT=5432
            - DB_DATABASE=postgres
            - DB_USERNAME=postgres

I’m not familiar with the environmental variables used by your Larval container. But, based on your example code, this would be the likely change.

If you are running your Larval containers outside of this compose file, there is documentation elsewhere that might be able to help you create/find the IP address and ports you would use from inside your Larval container to access the database.

1 Like

Yes, thanks. I’m posting my entire compose file now.

Laravel lives outside the containers. It’s just mapped to the php one through the src folder.

version: '2'

services:
    web:
        image: nginx:latest
        container_name: nginx-stubb
        ports:
            - "8001:80"
        volumes:
            - ./app:/src
            - ./env-config/site.conf:/etc/nginx/conf.d/default.conf
    php:      
        build: ./env-config/php/         
        container_name: php-stubb           
        volumes:
            - ./app:/src
            - ./env-config/php/log.conf:/usr/local/etc/php-fpm.d/zz-log.conf          
            - ./env-config/php/php.ini:/usr/local/etc/php/php.ini 
    postgresql:
        image: postgres
        container_name: postgres-stubb
        ports:
            - "5431:5432"       
        environment:         
            - POSTGRES_DB=postgres
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=d4REn0LdCH4B
        volumes:
            - ./env-config/postgres.conf:/etc/postgresql.conf
            - /srv/docker/postgresql:/var/lib/postgresql    
        command: postgres -c config_file=/etc/postgresql.conf
        restart: always

So, Laravel is calling 5431 port in mi PC and it’s not finding it open or something (same for 5432). However if I use netstat to check the port it seems to be fine:
netstat -antup | grep 5431
returns
tcp6 0 0 :::5431 :::* LISTEN -

As the port fails in my pc this should be a Docker unrelated issue right? I was just guessing why Docker is not forwarding the call to 5432 inside postgres container.

I will keep researching the topic outside the forum. It’s just that I not being able to find the problem anywhere.

Thanks.

P.S: Could it be that 127.0.0.1 is the wrong IP when running a PHP application inside a container? Thas it refer to the container IPs or my PCs ones?

127.0.0.1 (and its reserved DNS name localhost) always refers to the current container, never the host.

1 Like

Oh! So instead of localhost or 127.0.0.1 I must use the container name. Now I remember.

Thanks so much.

1 Like

Hi,

I did a quick internet search on the error message. The results mainly relate to local socket vs internet socket configurations. However, because the Postgres container was packaged for Docker, it would seem likely it would be configured correctly for the container environment.

If you have your own custom configuration in the configuration file you mount, postgress might be using a local socket rather than an internet socket. A common configuration when the database and web server are on the same server.

In the configuration file:
/var/lib/pgsql/data/postgresql.conf

Is this set?
tcpip_socket = true

It looks like Larval may be mounted from the host machine, but is in reality being run inside one of the containers. Or so it seems.

Because Postgres is not running in the same container as your web or php containers/services, trying to connect to postgres through 127.0.0.1 and the port number you exposed, within another container, does not seem correct, as that is the localhost for the container, not the host machine. and likely would not have the port you exposed from the postgres container on it.

You may want to try DB_HOST=postgresql for the connection. The private network DNS server should resolve that to the correct address (usually a 172.17.xxx.xxx private address). If you do a “docker network ls” you should see a bridge network ending with “_default”. That is the network Docker created for the containers to talk to each other.

If you are currently locked into an older version of Docker, use “link” as that system will supply the IP address that web or php container need to use to connect to the database through an environmental variable scheeme.

Hopefully one of these items is the problem.

1 Like

As Roseanne Roseannadanna used to say “Never Mind”. I started this reply several hours ago, was diverted, and only just now got back to finishing it up and hitting the send button. Had not seen your reply, because the edit window was open.

Since the three services above are under the same network,just map the requested host with “db_name” would work.

Hello dude, i have the same problem, can you help me? thanks!

$ psql
psql: error: could not connect to server: FATAL:  database "user" does not exist
$ psql -U postgres
psql: error: could not connect to server: FATAL:  database "postgres" does not exist

It already inited database, why postgres does not exist?

It already inited database, why postgres does not exist?

It already inited database, why Lucky Patcher Kodi postgres does not exist?

This resolved my issue when trying to connect through pgadmin, in connection details I was trying to connect with (localhost/127.0.0.1 …) ips but when I put posgres container name it worked !!
Thanks guys !!

Thank you, saved my week.