Home>

I'm developing in a local environment using docker and using postgresql. I want to see the value of db in local development environment via postico

could not connect to server: Connection refused
    Is the server running on host "localhost" (:: 1) and accepting
    TCP/IP connections on port 5433?
could not connect to server: Connection refused
    Is the server running on host "localhost" (fe80 :: 1) and accepting
    TCP/IP connections on port 5433?
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 5433?

And will come out. dockerfile etc. are the following settings. I am developing with rails.

version: '3'
services:
  db:
    image: postgres: 9.5.18
    volumes:
      -./docker/pg:/var/lib/postgresql
    environment:
      POSTGRES_PASSWORD: password
      POSTGRES_USER: localhost
      POSTGRES_DB: test_db
    ports:
      -'5433: 5432'
  web:
    build:.
    command: bash -c "rm -f tmp/pids/server.pid&&bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      -.:/myapp
    ports:
      -"3000: 3000"
    depends_on:
      -db

database.yml

default:&default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5
development:
  <<: * default
  database: myapp_development
test:
  <<: * default
  database: myapp_test

postico settings are below

  

  • Answer # 1

    I copied and pasted it into docker-compose.yml and tried it, but it connected normally.


      

    The steps for carving are:

    Try with psql instead of an app

    $psql -U localhost -d test_db -h localhost -p 5433
    Password for user localhost:
    psql (12.2, server 9.5.18)
    Type "help" for help.
    test_db = # \ q

    If this works, postico itself and its settings are suspicious.
    If not, go to 2.

    2. Connect and view from inside the container

    $docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    f0b3ad031173 postgres: 9.5.18 "docker-entrypoint.s…" 9 hours ago Up 12 minutes 0.0.0.0:5433->5432/tcp StackOverflow_247008_db_1
    $docker exec -it StackOverflow_247008_db_1 bash
    root @ f0b3ad031173:/# psql -U localhost -d test_db -h localhost -p 5432
    psql (9.5.18)
    Type "help" for help.
    test_db = # \ q

    The name of the "StackOverflow_247008_db_1" part attached after docker exec -it changes depending on the environment, so use the name of the NAMES part (far right end) that came out with docker ps

    If this works, the settings per docker network are suspicious.

        ports:
          -'5433: 5432'

    If you change the port number, it is easy to make mistakes in the port specification, so try using 5432: 5432.

    On the other hand, if even psql in the container is not connected, starting postgres itself becomes suspicious.

    root @ f0b3ad031173:/# ps aux
    USER PID% CPU% MEM VSZ RSS TTY STAT START TIME COMMAND
    postgres 1 0.0 1.1 274544 23084? Ss 20:56 0:00 postgres
    postgres 23 0.0 0.1 274544 3696? Ss 20:56 0:00 postgres: checkpointer process
    :

    Check if there is a postgres process

    If there seems to be only bash and ps aux, it is highly likely that the startup has failed, so get out of the container and from outside

    $docker-compose logs db

    Let's look at the log at startup.

  • Answer # 2

    If you connect with the first docker-compose run, you should do as follows.

    docker-compose run db psql -U localhost -d test_db -h db -p 5432

    It's a mystery that you can't connect after logging in with the second docker-compose exec. . .
    It's better to check if you're listening properly as lirlia said.
    docker posgres seems to be running on debian, so you can check it by installing the following package:

    apt-get update
    apt-get install net-tools
    netstat -antl

  • Answer # 3

    It was a rudimentary mistake that the username in databse.yml did not match the POSTGRES_USER in docker-compose.yml. Thank you for your cooperation!