在 GoLang 中运行 SQL 查询时不存在 PQ 关系 [英] PQ Relation does not exist when running SQL query in GoLang

查看:137
本文介绍了在 GoLang 中运行 SQL 查询时不存在 PQ 关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 GoLang 中对我的数据库运行任何 sql 查询时,我收到以下错误,有人知道我是否可以尝试解决其他任何问题

When running any sql query to my database in GoLang, i get the below error, does anyone know if there's anything else I can try to resolve the issue

pq: relation "users" does not exist

我尝试过的:

检查了数据库的权限,一切正常.
检查凭据没问题
通过 psql
直接在 SQL 数据库上运行 INSERT 语句创建了一个极简程序以确保它不是我的代码中的任何其他内容
手动将值输入到查询字符串中,而不是使用参数
试过 SELECT 语句和同样的问题.它只是没有找到用户表.
我已经通过 const 变量构建了连接字符串,如下所示.两者都没有工作.
我试过有和没有端口(5432)

Checked the permissions on the DB and all is okay.
Checked the credentials are okay
Ran the INSERT statement directly on the SQL database via psql
Created a minimalist program to make sure it wasn't anything else in my code
Entered values into query string manually, rather than using parameters
Tried SELECT statement and same issue. It just isn't finding the users table.
I have built the connection string through const variables, and as I have below. Neither have worked.
I have tried with, and without the port (5432)

下面的代码是我所拥有的(极简应用程序输出与我的主应用程序相同的错误)

The code below is what I have (the minimalist application outputting the same error as my main application)

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 环境中,自从搬过来……我遇到了这个问题.

I expect that the above code isn't incorrect, as I had this working in my windows environment, but I don't want to use windows for development. This is all in my Linux environment, and since moving over... I have encountered this issue.

以下是 postgres 用户对数据库的权限

Below are the permissions for postgres user on database

https://i.imgur.com/ZqFUrek.png

推荐答案

您的连接字符串:

postgres://postgres:postgres@localhost/user_details?sslmode=disable

表示您正在连接到 user_details 数据库.也许您在不同的数据库中创建了 users 表.

says that you're connecting to the user_details database. Perhaps you created the users table in a different database.

这篇关于在 GoLang 中运行 SQL 查询时不存在 PQ 关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆