gorm: 拨号tcp 127.0.0.1:5432: 连接: 连接被拒绝

huangapple go评论83阅读模式
英文:

gorm: dial tcp 127.0.0.1:5432: connect: connection refused

问题

我正在尝试使用以下Docker配置连接到我的数据库,并使用gorm进行操作:

services:
  app:
    build: 
      context: .
      # 将路径更正为您的Dockerfile的路径
      dockerfile: Dockerfile
    ports:
      - "127.0.0.1:8080:8080"
    volumes:
      - .:/app
    networks:
      - backend
    depends_on:
      - db
  
  db:
    container_name: simple_api_db_user
    restart: always
    image: postgres:latest
    environment:
      POSTGRES_USER: mateen
      POSTGRES_PASSWORD: password
      POSTGRES_DB: simple_api_db_user
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - backend
    ports:
      - "127.0.0.1:5430:5430"
  
volumes:
  db_data:

networks:
  backend:
    driver: bridge

这是我在Golang中连接到PostgreSQL的代码:

func NewPostgresConnection() (*gorm.DB, error) {
	psqlConfig, err := dbConfigs.NewPostgresqlConfig()
	if err != nil {
		return nil, err
	}

	db, err := sql.Open("postgres", psqlConfig.Dsn())
	if err != nil {
		log.Println("DB Connection error: ", err.Error())
		return nil, err
	}

	if err := db.Ping(); err != nil {
		log.Println("DB Ping error: ", err.Error())
		return nil, err
	}

	gdb, err := gorm.Open(postgres.Open(psqlConfig.Dsn()), &gorm.Config{})
	if err != nil {
		return nil, err
	}

	err = Migrate(gdb)
	if err != nil {
		return nil, err
	}

	return gdb, nil
}

这是我打印出来的配置DSN:

&{host=simple_api_db_user user=mateen password=password dbname=simple_api_db_user port=5430 sslmode=disable TimeZone=UTC}

当我运行我的应用程序时,我遇到了以下错误:

simple-api-user-app-1  | 2023/05/10 14:05:16 DB Ping error: dial tcp 172.31.0.2:5430: connect: connection refused
simple-api-user-app-1  | 2023/05/10 14:05:16 dial tcp 172.31.0.2:5430: connect: connection refused

这是我的Dockerfile,尽管我认为它可能与问题无关:

# Builder
FROM golang:latest

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

RUN go install github.com/cosmtrek/air@latest

COPY . .

CMD ["air"]

EXPOSE 8080

PS: 当我尝试在DBeaver中连接到5430端口的PostgreSQL时,我遇到了以下错误:

The connection attempt failed.
  EOFException
  java.io.EOFException

编辑:更改了

英文:

I am trying to connect to my database created by the following docker configs using gorm:

services:
  app:
    build: 
      context: .
      # Correct the path to your Dockerfile
      dockerfile: Dockerfile
    ports:
      - "127.0.0.1:8080:8080"
    volumes:
      - .:/app
    networks:
      - backend
    depends_on:
      - db
  
  db:
    container_name: simple_api_db_user
    restart: always
    image: postgres:latest
    environment:
      POSTGRES_USER: mateen
      POSTGRES_PASSWORD: password
      POSTGRES_DB: simple_api_db_user
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - backend
    ports:
      - "127.0.0.1:5430:5430"
  
volumes:
  db_data:

networks:
  backend:
    driver: bridge

here is the code to my postgres connection in golang:

func NewPostgresConnection() (*gorm.DB, error) {
	psqlConfig, err := dbConfigs.NewPostgresqlConfig()
	if err != nil {
		return nil, err
	}

	db, err := sql.Open("postgres", psqlConfig.Dsn())
	if err != nil {
		log.Println("DB Connection error : ", err.Error())
		return nil, err
	}

	if err := db.Ping(); err != nil {
		log.Println("DB Ping error: ", err.Error())
		return nil, err
	}

	gdb, err := gorm.Open(postgres.Open(psqlConfig.Dsn()), &gorm.Config{})
	if err != nil {
		return nil, err
	}

	err = Migrate(gdb)
	if err != nil {
		return nil, err
	}

	return gdb, nil
}

and here is my config DSN when I print it out:
&{host=simple_api_db_user user=mateen password=password dbname=simple_api_db_user port=5430 sslmode=disable TimeZone=UTC}

I am getting the following error when running my app:

simple-api-user-app-1  | 2023/05/10 14:05:16 DB Ping error:  dial tcp 172.31.0.2:5430: connect: connection refused
simple-api-user-app-1  | 2023/05/10 14:05:16 dial tcp 172.31.0.2:5430: connect: connection refused

Here is my Dockerfile even though I think it might be irrelevant:

# Builder
FROM golang:latest

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

RUN go install github.com/cosmtrek/air@latest

COPY . .

CMD [ "air" ]

EXPOSE 8080

PS: When I try to connect to postgres port to 5430 in dbeaver, I get the following error:

The connection attempt failed.
  EOFException
  java.io.EOFException

Edit: the changed

答案1

得分: 1

PostgreSQL容器正在监听5432端口。因此,ports选项应设置为5430:5432。这将将主机机器上的端口5430映射到Docker容器上的端口5432。

英文:

the postgres container is listening on 5432. so the ports options should be set to 5430:5432. this maps port 5430 on your host machine to 5432 on the docker container.

huangapple
  • 本文由 发表于 2023年5月10日 21:29:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定