A
A
Andrey Astafiev2016-10-28 14:59:18
go
Andrey Astafiev, 2016-10-28 14:59:18

What is the error when fetching from a Postgresql database?

Below is the program code

package main

import (
  "database/sql"
  _ "github.com/lib/pq"
  "log"
  "fmt"
)

const (
  DB_USER     = "postgres"
  DB_PASSWORD = ""
  DB_NAME     = "test"
)

type User struct {
  id int
}

func main() {
  dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
    DB_USER, DB_PASSWORD, DB_NAME)
  db, err := sql.Open("postgres", dbinfo)
  defer db.Close()

  if err != nil {
    log.Fatal(err)
  }

  rows, err := db.Query(" SELECT * FROM users ")
  defer rows.Close()

  if err != nil {
    log.Fatal(err)
  }else{
    if(rows.Next()){
      usr := new (User)
      err := rows.Scan(&usr.id)
      if err != nil {
        log.Fatal(err)
      }
      fmt.Println(usr.id)
    }
  }

}

when run it gives the following error
pq: relation "users" does not exist
exit status 1

Although there is such a table and I don’t know what could be wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2016-10-28
@FireGM

You definitely don't have such a table. You either connected to the wrong place, or show /list of your database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question