Yash Kumar Atri
Yash Kumar Atri

Reputation: 795

Issue with INSERT into table

I have successfully established connection with Postgres and GoLang but whenever I insert data in to the table it throws an error of not finding the table

Go Code

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", "[email protected]").Scan(&id)
    if err != nil {
        panic(err)
    } 

The ouput I get in console is

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

My postgres table name

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

postgres=# 

What am I doing wrong??

Upvotes: 2

Views: 541

Answers (1)

Vao Tsun
Vao Tsun

Reputation: 51416

you connect to database test

dbname   = "test"

and you have table in database postgres

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

Upvotes: 2

Related Questions