在 GoLang 中对我的数据库运行任何 sql 查询时,我收到以下错误,有谁知道是否还有其他任何我可以尝试解决的问题
pq: relation "users" does not exist
我尝试过的:
检查了数据库的权限,一切正常。
检查凭据没问题
通过 psql 直接在 SQL 数据库上运行 INSERT 语句
创建了一个极简程序以确保它在我的代码中没有任何其他内容
手动将值输入查询字符串,而不是使用参数
试过 SELECT 语句和同样的问题。它只是找不到用户表。
我已经通过 const 变量构建了连接字符串,如下所示。两者都没有用。
我尝试过使用和不使用端口 (5432)
下面的代码是我所拥有的(极简应用程序输出与我的主应用程序相同的错误)
func main() {
sqlInfo := fmt.Sprintf("postgres://postgres:postgres@localhost/user_details?sslmode=disable")
db, err := sql.Open("postgres", sqlInfo)
defer db.Close()
if err != nil {
fmt.Fprintf(os.Stdout, "Connection to the database failed")
return
}
err = db.Ping()
if err != nil {
fmt.Fprintf(os.Stdout, "Connection to the database failed")
return
}
fmt.Fprintf(os.Stdout, "You have connected to the database successfully")
sqlQuery := `INSERT INTO users ("user_id", "username", "password", "email", "gender", "gang") VALUES ($1, $2, $3, $4, $5, $6)`
_, err = db.Exec(sqlQuery, 1, "Ross8839", "rocky8839", "ross88399@hotmail.com", "Female", "Greengos")
if err != nil {
fmt.Fprintf(os.Stdout, "Failed to query db")
panic(err)
}
}
我希望上面的代码没有错误,因为我在我的 windows 环境中工作,但我不想使用 windows 进行开发。这都是在我的 Linux 环境中,自从搬过来之后……我遇到了这个问题。
以下是postgres用户对数据库的权限
最佳答案
您的连接字符串:
postgres://postgres:postgres@localhost/user_details?sslmode=disable
表示您正在连接到 user_details
数据库。也许您在不同的数据库中创建了 users
表。
关于postgresql - 在 Go 语言中运行 SQL 查询时 PQ 关系不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54598398/