INSERT进入表的问题

时间:2018-02-02 12:08:53

标签: postgresql go

我已经成功建立了与Postgres和GoLang的连接,但每当我将数据插入表中时,都会抛出错误,找不到表

转码

const (
    host     = "localhost"
    port     = 5432
    user     = "postgres"
    password = "root"
    dbname   = "test"
)

func main() {
    psqlInfo := fmt.Sprintf("host=%s port=%d 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)
    }
    fmt.Println("Connection Success")

    sqlStatement := `
INSERT INTO users (id, age, first_name, last_name, email)  
VALUES ($1, $2, $3, $4, $5)  
RETURNING id`
    id := 0
    err = db.QueryRow(sqlStatement, 30, 26, "firstname", "lastname", "Calhoun@mail.com").Scan(&id)
    if err != nil {
        panic(err)
    } 

我在控制台中获得的输出是

yashkumar@atri-HP-15-Notebook-PC:~/Documents/Feb2.18/src$ go run dbin.go
Connection Success
panic: pq: relation "users" does not exist

goroutine 1 [running]:
main.main()
        /home/yashkumar/Documents/Feb2.18/src/dbin.go:40 +0x6be
exit status 2

我的postgres表名称

postgres=# \dt
         List of relations
 Schema | Name  | Type  |  Owner   
--------+-------+-------+----------
 public | users | table | postgres
(1 row)

postgres=# 

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您连接到数据库测试

dbname   = "test"

你在数据库 postgres

中有表格
postgres=# \dt
         List of relations
 Schema | Name  | Type  |  Owner   
--------+-------+-------+----------
 public | users | table | postgres
(1 row)