[Solved] Relation <table name> does not exist

[EDIT] Thanks marc for your suggestion. So far it seems to have worked!

Hi! Firstly, I apologise if this is not the correct way to post a bug report (I’m not even certain it’s a bug). I’m currently mid-development on a Go project, and I’m using a single instance of Cockroach for the database (loving it so far).

I’m connecting to this Cockroach instance via a program written in Go, using the “github.com/lib/pq” library.

Each time I launch, the init sequence works without any issues (Create database if not exists, Set database = x, Create table if not exists, etc.). I only use fairly basic queries when interacting with the database.

Eventually, after an indeterminate amount of time (anywhere from 1-3 hours), my queries begin to return “relation

does not exist”. This isn’t just for a single specific table; it happens to any table that my queries are performed on.I had suspected that perhaps the connection to the database had been lost, so I implemented a function that periodically (every 20 seconds) pings the database and ensures that the connection still exists. So far, this has yet to return any errors.

When connecting, I use the following string: postgresql://x@y:26257?sslmode=disable
(with x and y obscuring the user information).

I’m not sure what other information I can provide that would be useful, but let me know if there is and I’ll do my best! Thanks for any advice :slight_smile:

This is most likely due to new connections being created. When that happens, a new sessions is initialized from the connection string. Since there is no database name in your connection string, the new session requires a SET database = ... call on it.

It’s recommended to specify the database name in your connection string. eg: postgresql://user@host:26257/dbname?sslmode=disable. This will ensure that new sessions will automatically have the correct database name populated.

Thanks for the super-fast reply!
I’ll give that a go, and let you know if it ends up resolving my issues. It may take some time before I can say it with any certainty, though. Thanks again :smiley:

Out of interest: what is the expected behaviour when connecting to Cockroach with a string like the one you provided, if the specified database does not exist? I’ve run it in a test program, and no errors are thrown.

James, if your connection string specifies a database that does not exist, the connection will be established but most queries will fail with an error message that indicates the database does not exist. The errors will then subsume once you use CREATE DATABASE.

Hope this helps