dtf24224
2019-02-08 18:37
浏览 645

在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

图片转代码服务由CSDN问答提供 功能建议

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

  pq:关系“用户”不存在
   
 
 

我所拥有的 尝试过:

检查了数据库的权限,一切正常。
检查了凭据是否正常
直接通过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,“与数据库的连接失败”)
  return 
} 
err = db.Ping()
 
if err!= nil {
 fmt.Fprintf(os.Stdout,“与数据库的连接失败”)
返回
} 
 
fmt。  Fprintf(os.Stdout,“您已成功连接到数据库”)
 
sqlQuery:=`INSERT INTO用户(“ user_id”,“用户名”,“密码”,“电子邮件”,“性别”,“帮派”  )值($ 1,$ 2,$ 3,$ 4,$ 5,$ 6)`
_,err = db.Exec(sqlQuery,1,“ Ross8839”,“ rocky8839”,“ ross88399@hotmail.com”,“女性”,“  Greengos“)
if err!= nil {
 fmt.Fprintf(os.Stdout,”数据库查询失败“)
 panic(err)
} 
} 
   
 
 

我希望上面的代码是不正确的,因为我已经在Windows环境中运行了此代码,但是我不想使用Windows进行开发。 这一切都在我的Linux环境中,并且由于迁移而已...我遇到了这个问题。

以下是数据库中postgres用户的权限 < p> https://i.imgur.com/ZqFUrek.png \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douhuangjian9627 2019-02-09 04:26

    Your code seems to have no issue and seems to be a working one. I highly doubt if the table users is actually present in the selected database user_details.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题