2

The main problem is that i cannot run postgresql even on vm with the error:

root@a2c8a58d4e0e:/# psql -h localhost -U psqluser -W
Password for user psqluser:
psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?

For that purpose i run this commands inside VM:

pg_createcluster 9.6 main --start
/etc/init.d/postgresql start

And then it works properly on VM. But that's manually.

I configured everything by official docker repo docs.

This is my docker compose file:

version: "3.3"

services:
  postgresql:
    build:
      context: .
      dockerfile: postgresql
    container_name: Postgres
    restart: always
    ports:
    - "5432:5432"
    environment:
      POSTGRES_DB: 'psqldb'
      POSTGRES_USER: 'psqluser'
      POSTGRES_PASSWORD: 'temp123'
    volumes:
    - /home/VOLUMES/DB/Postgresql:/var/lib/postgresql

I did inheritance from original repo as i want to run postgresql service automatically. Otherwise it's not running.

postgresql file:

FROM postgres:9.6.11
RUN pg_createcluster 9.6 main --start
RUN /etc/init.d/postgresql start

It does not run Postgres as well. Only manually inside VM.

What's wrong?

1 Answer 1

1

Within docker compose, ports aren't exposed on the host by default. https://docs.docker.com/compose/compose-file/

ports is virtual within the docker compose network. If you want to expose them to the host machine, you can use the expose option instead of ports.

Alternatively, you can also run docker-compose run with the --service-ports flag which will automatically expose the ports to the host when running.

docker-compose run --service-ports postgresql (see doc)

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

2 Comments

tried without ports and still getting root@85e21cca0d93:/# psql -h localhost -U psqluser -W Password for user psqluser: psql: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
From the error message: "Is the server running on host "localhost" (127.0.0.1)"? Is it? It is with docker-for-mac but for different setup it can be different. Then about the ports option; you must not remove it. Either replace it with expose or run with --service-ports flag.

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.