Tidy way to check for database table existence (in a non-default schema)?



What is the tidy way to check for table existence in the non-default schema of a remote db? I know I can do something like…

my_src <- src_postgres(<db connection details>)
my_table <- my_src$con %>% tbl(in_schema("schemaname", "tablename"))

…to get a given table from a particular schema. However, if the table doesn’t exist I get an error. Rather than error handling via tryCatch I’m hoping for some way to determine the (non-)existence of the non-default schema table in advance. It seems like there must be something, I’m just coming up short on search terms.


dbExistsTable() can do this.

con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
copy_to(con, mtcars)

con %>% DBI::dbExistsTable("mtcars")
con %>% DBI::dbExistsTable("iris")


Sorry, I did a horrible job of writing that initial post. I’ll fix it up some. The problem I’m having is checking for the existence of a table in particular schema.