1

panic: dial tcp 192.168.0.2:5432: connect: connection refused

package database

import (
    "database/sql"
    "fmt"

    _ "github.com/lib/pq"

    "WebApp/config"
)

func Connect() (*sql.DB, error) {

    psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
        config.DB_HOST, config.DB_PORT, config.DB_USER, config.DB_PASSWORD, config.DB_NAME)

    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err)
    }
    fmt.Printf("\nSuccessfully connected to database!\n")
    return db, nil

}
version: '3'

services:

  backend:
    build:
      context: ../RestAPI-Golang
      dockerfile: Dockerfile.dev
    environment:
      - DB_USER=username
      - DB_PASSWORD=password
      - DB_NAME=default_database
      - DB_PORT=5432
      - DB_HOST=database
    ports:
      - "3000:3000"
    volumes:
      - ../RestAPI-Golang:/app
    depends_on:
      - database

  database:
    image: postgres
    restart: always
    volumes:
      - ./db-data/:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=username 
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=default_database 
    ports:
      - "5432:5432"
      
  db-admin:
    image: adminer
    ports: 
      - 8080:8080

I've tried using local host instead of the database container name, using the container ip, and other options online but I have had no luck. I believe the docker container name resolves to the ip of the container? And since it's communication within the same docker network I do not need to expose any additional ports?

  • 1.you can use the ip which run docker(exposed 5432) 2.you can use [links/network](https://runnable.com/docker/docker-compose-networking) – Para Feb 17 '23 at 02:21
  • I still am getting the same error with both options. It panics after I call db.ping() still. – Michael Jones Feb 17 '23 at 02:58
  • Make sure the `config` values are what you think they are. database:5432 should be able to connect. – Burak Serdar Feb 17 '23 at 03:15
  • @BurakSerdar this is the result of printing psqlinfo "host=192.168.80.3 port=5432 user=username password=password dbname=default_database sslmode=disable" – Michael Jones Feb 17 '23 at 03:17
  • Is the IP of `database` container 192.168.80.3? – Burak Serdar Feb 17 '23 at 03:41
  • @BurakSerdar I just checked its 192.168.144.3 – Michael Jones Feb 17 '23 at 03:52
  • Just tried the same thing using gorm and I got the same error. – Michael Jones Feb 17 '23 at 04:32
  • Then shouldn't you be connecting to 192.168.144.3:5432 ? – Burak Serdar Feb 17 '23 at 05:13
  • Yeah, the IP changes on every new run of docker-compose so that was from a newer run. – Michael Jones Feb 17 '23 at 05:18
  • If you run `docker-compose up` without `-d`, you'll see the containers' log messages; is the database fully started up before the application tries to connect to it? (Compiled Go applications can start extremely quickly, but a database often needs 30-60 seconds to be functional.) Also see [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y). – David Maze Feb 17 '23 at 11:35

1 Answers1

0

use wait-for.sh to docker-compose like this:

    entrypoint:
      [
        "/app/wait-for.sh",
        "postgres:5432",
        "--",
        "/app/start.sh"
      ]
jigsaw373
  • 26
  • 2