dtf24224 2019-02-08 18:37
浏览 1022
已采纳

在GoLang中运行SQL查询时,PQ Relation不存在

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

What I have tried:

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)
}
}

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.

Below are the permissions for postgres user on database

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

  • 写回答

2条回答 默认 最新

  • dsfds4551 2019-02-09 03:05
    关注

    Your connection string:

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器