Freda K
Freda K

Reputation: 464

Why can I not read my table although it is listed by dbListTables?

I am trying to read a table into R using RPostgreSQL and R v2.14.2.
My version of RPostgreSQL is listed as 0.3-2, downloaded 16-May-2012.
My version of DBI is listed as 0.2-5, downloaded 16-May-2012.

I can open the database, and list the tables. The table I want to open is clearly present, however, when I try to read it, I get an error message. I am unsure if the error is in my code or in the way the database is set up.

library(RPostgreSQL)  
# Loading required package: DBI  
drv <- dbDriver("PostgreSQL")  
con <- dbConnect(drv, host = 'freda.freda.com', dbname = 'test', user = 'fredak', password = 'xxxx')  

dbListTables(con)  
# [1] "chemistry”                                               
# [2] "ecog”  
# [3] "hematology"                                        

dbExistsTable(con, "ecog")  
# [1] FALSE

MyTable <- dbReadTable(con, "ecog")    
# Error in postgresqlExecStatement(conn, statement, ...) :  
#   RS-DBI driver: (could not Retrieve the result : ERROR:  relation "ecog" does not exist  
# LINE 1: SELECT * from "ecog"  
#                       ^  
# )  
# Error in names(out) <- make.names(names(out), unique = TRUE) :   
#   attempt to set an attribute on NULL  
# In addition: Warning message:  
# In postgresqlQuickSQL(conn, statement, ...) :  
#   Could not create executeSELECT * from "ecog"

Upvotes: 5

Views: 5627

Answers (3)

Arthur Yip
Arthur Yip

Reputation: 6230

The equivalent RPostgres syntax is

dbExistsTable(con, Id(schema = "schema_name", table = "table_name"))

Upvotes: 0

U W
U W

Reputation: 1290

If wanting to interact with a table that's in a named schema, use the following (unintuitive) syntax:

dbExistsTable(con, c("schema_name", "table_name"))
[1] TRUE

This works despite dbListTables(con) returning all table names without their associated schemas.

Upvotes: 14

Dirk is no longer here
Dirk is no longer here

Reputation: 368629

I suspect a permissions issue. Please try the SQL commands via psql or another venue to rule out any backend-permissions issues.

Your commands work fine for me here:

R> library(RPostgreSQL)
Loading required package: DBI
R> drv <- dbDriver("PostgreSQL")
R> con <- dbConnect(drv, dbname="beancounter", user="edd", password="xxxxxx") 
R> dbListTables(con)
[1] "beancounter"   "cash"          "fxprices"      "indices"       "meta"
[6] "portfolio"     "portfoliosold" "stockinfo"     "stockprices"  
R> dbExistsTable(con, "cash")
[1] TRUE
R> dbExistsTable(con, 'cash')
[1] TRUE
R> dbExistsTable(con, 'Cash')
[1] FALSE
R> dbExistsTable(con, "Cash")
[1] FALSE
R> ccc <- dbReadTable(con, "cash")
R> dim(ccc)
[1] 24  7
R> 

Upvotes: 0

Related Questions