Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lord_Gordon's avatar

Trouble Configuring my Environment with Docker and PostgreSQL

Hello everyone. I have been given an exercise to do, but I am having trouble setting up the work environment.

For this exercise I must download a docker image onto my machine and establish a connection with my local postgresql. Then I have several tasks to do working on this database with SQL queries.

..The problem is that after pulling the docker image and executing this command:

/* docker compose -f docker-compose.postgres.yml up */

I then go over to postgresql and put the username and passoword I was instructed to, but I dont know what to put in the host name/address field. When I put 'localhost' it says 'unable to connect to server'.

Any help would be much appreciated,

Regards,

0 likes
5 replies
LaryAI's avatar
Level 58

It sounds like you are having trouble connecting to the PostgreSQL server running in the Docker container. To connect to the PostgreSQL server running in the Docker container, you will need to use the IP address of the Docker container. You can find the IP address of the Docker container by running the following command:

docker inspect <container_name> | grep IPAddress

Once you have the IP address of the Docker container, you can use that IP address as the hostname/address when connecting to the PostgreSQL server.

If you are still having trouble connecting to the PostgreSQL server, you may need to check the configuration of the PostgreSQL server in the Docker container. You can do this by running the following command:

docker exec -it <container_name> psql -U <username>

This will open a psql shell in the Docker container, where you can check the configuration of the PostgreSQL server.

I hope this helps!

1 like
Lord_Gordon's avatar

@LaryAI Unfortunately neither of those 2 things work:-/

I succesfully managed to find the ip address of the container using the first command that you wrote there, but it fails to connect and produces a timeout error,

also the second command to set up a psql shell in the docker container.. i tried to type that command in the command line in vsc and it produced this error:

/* psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres" */

Regards,

Lord_Gordon's avatar

P.S I also tried putting my ip address in that field to no avail. I'm using pgdmin 4 by the way.

cwhite's avatar

Why don't you just spin up a postgres container in docker?

Note sure what your compose file looks like, but I can access the postgres container from PgAdmin with this:

version: '3.9'
services:
  postgres:
    container_name: 'postgres-db'
    image: postgres:14-alpine
    restart: unless-stopped
    ports:
      - '${DB_PORT:-5432}:5432'
    environment:
      SERVICE_NAME: postgres
      SERVICE_TAGS: dev
      POSTGRES_DB: ${DB_DATABASE:-postgres}
      POSTGRES_USER: ${DB_USERNAME:-postgres}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-docker}
    volumes:
      - './docker/postgres/:/docker-entrypoint-initdb.d/'
      - 'postgres:/var/lib/postgresql/data'
    networks:
      - app-network
networks:
  app-network:
    driver: bridge
volumes:
  postgres:
    driver: local

Please or to participate in this conversation.