Code to open connection to DB:
DB, err = sql.Open("postgres", "dbname=dev user=postgres password=postgres port=5400 sslmode=disable")
I'm trying to do a simple select query like:
err := DB.QueryRow("SELECT id FROM user WHERE id = $1", id)
but I get the error: pq: column id does not exist
However, if I try with the schema:
err := DB.QueryRow("SELECT id FROM public.user WHERE id = $1", id)
it goes fine. I don't understand why I must write the schema even though the default schema is public in the postgresql I'm running on Docker
useris a reserved keyword of postgres that is used to get information about the current database user. When you run a query likeSELECT * FROM user, it retrieves information about the current database user, not the data from a table nameduser.But with
SELECT id FROM public.userthis, it will get the result fromusertable ofpublicschema. This ensures that PostgreSQL looks specifically in thepublicschema for theusertable and retrieves the desired data.Solutions
1 - Rename your table into
users2 - Or use the schema name along with the query.
References :