How can I specify the schema from where to read a table in the st_read_db() function in the sf package in R?

When I try


drv = dbDriver("PostgreSQL")
con = dbConnect(drv, dbname = DBname_1, user = DBuser,
                host = DBhost, port = DBport, password = DBpassword)

dat = st_read_db(con, table = "schema1.table1",
      query = "SELECT * FROM schema1.table1 LIMIT 20")


I get the error:

Error: public.schema1.table1 does not exist.

It seems with this function I can only read tables from the public schema. Is this true or do I overlook something? Is this also maybe due to the early stage of the package?


Try :

dat <- st_read_db(con, table = c("schema1", "table1"),
                  query = "SELECT * FROM schema1.table1")

NB : here query is useless as you're selecting all rows

  • Thanks for the hint of putting the table information into a vector. However when I run your code it ignores the query argument. Note: I've changed the query to not represent the whole table. – andschar Apr 21 '17 at 10:46
  • And dat <- st_read_db(con, query = "SELECT * FROM schema1.table1 LIMIT 20") ? – mdag02 Apr 21 '17 at 10:57
  • returns the warning: argument table missing: returning object without crs but when I use head(dat) it shows me the right Multipolygons and I can plot 'em.. so it works. – andschar Apr 21 '17 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.