[英]Cannot connect to RDS Postgres using Golang Gorm
I got this error when trying to connect to my RDS Postgres Endpoint尝试连接到我的 RDS Postgres 端点时出现此错误
dial tcp 172.xx.xx.x:5432: i/o timeout panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x98 pc=0x1600d16] dial tcp 172.xx.xx.x:5432: i/o timeout panic: runtime error: invalid memory address or nil pointer dereference [信号 SIGSEGV: 分段违规代码=0x1 addr=0x98 pc=0x1600d16]
Seems like there's some problem connecting to the endpoint连接到端点似乎存在一些问题
.Env file .env 文件
DB_HOST=dbname.asdasddsa.ap-southeast-1.rds.amazonaws.com
DB_PORT=5432
DB_USER=username
DB_NAME=dbname
DB_PASSWORD=password
My database.go file我的 database.go 文件
func InitDB() *gorm.DB {
loadEnv() // For environment variables
psqlInfo := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s",
os.Getenv("DB_HOST"),
os.Getenv("DB_USER"),
os.Getenv("DB_NAME"),
os.Getenv("DB_PASSWORD"),
)
db, err := gorm.Open("postgres", psqlInfo)
if err != nil {
fmt.Println(err)
return nil
}
fmt.Println("Connected to the Database")
DB = db
return DB
}
Do i need to configure anything in my RDS AWS?我需要在我的 RDS AWS 中配置任何东西吗? I followed this article我关注了这篇文章
https://aws.amazon.com/getting-started/tutorials/create-connect-postgresql-db/ https://aws.amazon.com/getting-started/tutorials/create-connect-postgresql-db/
and followed the exact steps for the configuration并遵循配置的确切步骤
Regards,问候,
I am using this function to connect to my postgres database on RDS我正在使用此功能连接到 RDS 上的 postgres 数据库
package database
import (
"fmt"
"github.com/jinzhu/gorm"
config "github.com/spf13/viper"
)
func Connect() (*gorm.DB, error) {
return gorm.Open(
"postgres",
fmt.Sprintf(
"host=%s port=%s user=%s dbname=%s password=%s",
config.GetString("postgres.host"),
config.GetString("postgres.port"),
config.GetString("postgres.user"),
config.GetString("postgres.dbname"),
config.GetString("postgres.password"),
),
)
}
Please check if you have access to your RDS database before running this.请在运行之前检查您是否有权访问您的 RDS 数据库。 You can check if you have access to the database from AWS console -> RDS -> database -> -> [scroll down] under Security group rules You can see if you have added access to the security group to access your database from your network.您可以从 AWS 控制台 -> RDS -> 数据库 -> -> [向下滚动] 在安全组规则下检查您是否有权访问数据库您可以查看是否添加了对安全组的访问权限以从您的网络访问您的数据库.
You can learn more about security groups here https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#VPCSecurityGroups您可以在此处了解有关安全组的更多信息https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#VPCSecurityGroups
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.