我try 使用github.com/jackc/pgx/v5包在AWS RDS中的Go程序中连接到Postgres数据库,但出现以下错误:

failed to connect to `host=xxxxx.xxxxxxx.us-east-1.rds.amazonaws.com user=golangpg database=golangpg`: server error (FATAL: database "golangpg" does not exist (SQLSTATE 3D000))
exit status 1

即使明确地说数据库不存在也是错误的,但我已经创建了RDS数据库,我也可以通过pgAdmin连接到该数据库.但当我试图连接我的GO代码时,我得到了上面提到的错误.

我已经判断了以下几点

  1. 安全组允许所有流量通过5432端口
  2. 在RDS中启用公共访问
  3. 能够通过pgAdmin连接到同一数据库.
     
    package database
    
    import (
        "context"
        "fmt"
        "os"
        "strconv"
    
        "github.com/jackc/pgx/v5"
        "github.com/joho/godotenv"
    )
    
    func PGConnection() *pgx.Conn {
        envErr := godotenv.Load()
        if envErr != nil {
            fmt.Println("Error in loading .env file, ", envErr)
        }
    
        host := os.Getenv("DB_URL")
        port := os.Getenv("DB_PORT")
        user := os.Getenv("DB_USERNAME")
        password := os.Getenv("DB_PASSWORD")
        dbname := os.Getenv("DB_NAME")
    
        portInt, er := strconv.Atoi(port)
        if er != nil {
            fmt.Println("Error in converting string to number", er)
        }
    
        var err error
    
        pgConnString := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=require", host, portInt, user, password, dbname)
    
        pgConn, err := pgx.Connect(context.Background(), pgConnString)
    
        if err != nil {
            fmt.Printf("Unable to connection to database: \n%v\n", err)
            os.Exit(1)
        } else {
            fmt.Println("Database connection establised to db ", dbname)
        }
    
        return pgConn
    }
    
    var Connection *pgx.Conn = PGConnection()

这里的错误可能是什么.即使在pgAdmin中,我也可以创建表格,但无法从GO代码进行连接.

enter image description here

enter image description here

推荐答案

这个问题是因为您的RDS PostgreSQL主机中的database "golangpg" does not exist.

RDS identifier != database name


Here I re-create your setup:

PostgreSQL RDS

enter image description here

Executed the same code and got the same error:

▶ go run main.go
Unable to connection to database: 
failed to connect to `host=golangdb.xxxx.eu-central-1.rds.amazonaws.com user=postgres database=golangdb`: server error (FATAL: database "golangdb" does not exist (SQLSTATE 3D000))
exit status 1

原因是我的PostgreSQL RDS中不存在database "golangdb" does not exist.

Connect to the database host and list the available databases.

▶ psql --host=golangdb.xxxxx.eu-central-1.rds.amazonaws.com --username=postgres

postgres=> \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype     
-----------+----------+----------+-----------------+-------------+-------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 
 rdsadmin  | rdsadmin | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 

这也是使用dbname := "postgres"可以工作的原因,因为数据库存在于主机中.

▶ go run main.go
Database connection establised to db  postgres

Create new database

postgres=> CREATE DATABASE golangdb;
CREATE DATABASE
postgres=> \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    
-----------+----------+----------+-----------------+-------------+-------------
 golangdb  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 
 rdsadmin  | rdsadmin | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 

Retry go code

▶ go run main.go
Database connection establised to db  golangdb

Postgresql相关问答推荐

将real[]数组作为完整的浮点值从postquist返回

使用regexp获取表名

在Go中,如何在没有数据库包的情况下运行PostgreSQL查询?

包含JSONB属性的过滤器的索引策略

如何在PostgreSQL中正确删除数据库

自左联接的POSTGIS更新

有没有办法共享数据或监控复杂 PostgresQL 事务的进度?

Postgres 函数 now() 返回不正确的时区偏移量

配置:错误:C 编译器无法在 Ubuntu 中创建可执行文件

如何使用 ActiveRecord json 字段类型

从 PostgreSQL 中的数字获取月份名称

如何将两个 PostgreSQL 列聚合到一个用括号分隔的数组

从局域网访问 PostgreSQL 服务器

从 Select 中创建一个临时表,如果表已经存在则插入

Postgres 日期重叠约束

如何从 CSV 为 PostgreSQL 副本生成模式

PostgreSQL 中跨多个表的索引

SQLAlchemy 声明式:定义触发器和索引 (Postgres 9)

如何将 CSV 数据插入 PostgreSQL 数据库(远程数据库)

如何在创建数据库时安装 Postgres 扩展?