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.
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.