4

When running any sql query to my database in GoLang, i get the below error, does anyone know if there's anything else I can try to resolve the issue

pq: relation "users" does not exist

What I have tried:

Checked the permissions on the DB and all is okay.
Checked the credentials are okay
Ran the INSERT statement directly on the SQL database via psql
Created a minimalist program to make sure it wasn't anything else in my code
Entered values into query string manually, rather than using parameters
Tried SELECT statement and same issue. It just isn't finding the users table.
I have built the connection string through const variables, and as I have below. Neither have worked.
I have tried with, and without the port (5432)

The code below is what I have (the minimalist application outputting the same error as my main application)

func main() {
sqlInfo := fmt.Sprintf("postgres://postgres:postgres@localhost/user_details?sslmode=disable")

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

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", "[email protected]", "Female", "Greengos")
if err != nil {
    fmt.Fprintf(os.Stdout, "Failed to query db")
    panic(err)
}
}

I expect that the above code isn't incorrect, as I had this working in my windows environment, but I don't want to use windows for development. This is all in my Linux environment, and since moving over... I have encountered this issue.

Below are the permissions for postgres user on database

https://i.sstatic.net/SQJ5n.png

1
  • I actually feed stupid. I think you're right. I created the database, and just assumed that I was creating the tables within the database. That's not what happened, and you're right. Thank you for your help. If you can answer the question, I will select it Commented Feb 8, 2019 at 19:44

2 Answers 2

11

Your connection string:

postgres://postgres:postgres@localhost/user_details?sslmode=disable

says that you're connecting to the user_details database. Perhaps you created the users table in a different database.

Sign up to request clarification or add additional context in comments.

Comments

0

Your code seems to have no issue and seems to be a working one. I highly doubt if the table users is actually present in the selected database user_details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.