Pq relation does not exist POSTGRES and GO


(Ross Bown) #1

I am trying to insert data into a table which i know is there, as the exact same query works directly on the database

GO CODE:

const (
host     = "localhost"
user     = "postgres"
password = "postgres"
dbname   = "user_details"
)

func connectDb() *sql.DB {
sqlInfo := fmt.Sprintf("host=%s user=%s "+
	"password=%s dbname=%s sslmode=disable",
	host, user, password, dbname)

db, err := sql.Open("postgres", sqlInfo)

fmt.Fprintf(os.Stdout, "\n%v", sqlInfo)

if err != nil {
	panic(err)
}

err = db.Ping()

if err != nil {
	panic(err)
}

fmt.Fprintf(os.Stdout, "You have successfully connected to the database")
return db
} 

The above code creates the connection which i call in the main func as db = connectDb setting the global db variable to the db which gets returned from the connectDB function. This appears to work, and connect okay using the built in ping method

The below code is the insert statement ran from the handleRegister func. I have done checks to make sure the db is still connected which it should be as I have a defer db.Close() in the main func to close the DB conn when the program exits.

	sqlSt := `INSERT INTO users ("User_id",  "username", "password", "email", "gender", "gang") VALUES ($1, $2, $3, $4, $5, $6)`
_, err = db.Exec(sqlSt, 1, ud.Username, pw, ud.Email, ud.Gender, ud.Gang)
if err != nil {
	panic(err)
}

below are the tables within my database.

Screenshot%20from%202019-02-07%2018-39-58

Any help appreciated

MININMAL program:

ERROR: Failed to query dbpanic: pq: relation “users” does not exist

package main

import (
"database/sql"
"fmt"
"os"

_ "github.com/lib/pq"
)

const (
host     = "localhost"
user     = "postgres"
password = "postgres"
dbname   = "user_details"
)

func main() {
sqlInfo := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable", host, user, password, dbname)

db, err := sql.Open("postgres", sqlInfo)

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)
}
}

(Norbert Melzer) #2

Any error?


(Ross Bown) #3

Yeah… the error is PQ (postgres) relation does not exist… it’s basically telling me the table ‘users’ doesn’t exist.


(Johan Dahl) #4

Write the most minimal program you can and test this it. Something like: https://goplay.space/#pcrciTaNPXW if that doesn’t work it must be something with the interface to the database. If you have this code in several places could it be something else. The devil is in the details.


(Cecil New) #5

Can you show describe users output too?


(Ross Bown) #6

I have updated the thread with the minimalistic program, which outputs the exact same error.


(Johan Dahl) #7

Does it work if you specify schema also:


sqlQuery := "INSERT INTO user_details.users ....

(Lutz Horn) #8

user_details is the name of the database, not the name of a schema. The schema is the default public. So I doubt that this helps.