doupafu6980 2019-01-17 19:07
浏览 197

Postgres Go查询给出错误关系表不存在

I have a Postgres table created with the following schema:

CREATE TABLE accounts
(
accountid       VARCHAR(56)  PRIMARY KEY,
balance         BIGINT       NOT NULL CHECK (balance >= 0),
seqnum          BIGINT       NOT NULL,
numsubentries   INT          NOT NULL CHECK (numsubentries >= 0),
homedomain      VARCHAR(32)  NOT NULL,
thresholds      TEXT         NOT NULL,
lastmodified    INT          NOT NULL
);

I'm using Postgres as my sql driver and using Go's native sql library. My main function looks like this:

func main() {
    psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err)
    }

    sqlStatement := "SELECT * FROM accounts WHERE accountid='123'"
    log.Println(sqlStatement)

    rows, err := db.Query(sqlStatement)
    if err != nil {
        panic(err)
    }
    defer rows.Close()
}

When I run this, I get the error relation "accounts" does not exist at character 15. The panic happens at the last error checking, hence the db.Query(sqlStatement) line. However, when I run the same query on the command line, results return correctly.

I read that this error often means the table name is not spelled correctly. I double checked this and it doesn't seem to be the case. I also checked that the user credentials I'm using have access right to the accounts table. It does, so I'm hoping some have had experience with this.

  • 写回答

1条回答 默认 最新

  • duanmian1085 2019-01-17 19:15
    关注

    To answer my own question, I had created the table under the wrong database. I double checked the user but not the database name I'm connecting to.

    评论

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上