2016-01-12 27 views
1

我是通过R连接到Redshift的新手,我已经阅读了其他问题,但尝试创建表格时仍遇到错误。 我已经成功地建立了一个连接,我想建立成功的表:Redshift with R

redshiftcon <- dbConnect(mm, user="username", password="secret_password", 
        dbname="dbtable", host="hostname", port="portnumber") 


dbSendQuery(redshiftcon, 
"create table ss_playground.test_table (unique_id VARCHAR, 
category VARCHAR, 
name VARCHAR, 
number_min float);") 

<PostgreSQLResult:(70214,5,1)> 

然而,当我试图检查表是否存在,如果场在那里,我得到以下信息:

dbExistsTable(redshiftcon, ss_playground.test_table) 

Error in is(object, Cl) : 
error in evaluating the argument 'name' in selecting a method for function 
'dbExistsTable': Error: object 'ss_playground.test_table' not found 

> dbExistsTable(redshiftcon, 'ss_playground.test_table') 
[1] FALSE 

我很困惑,因为我认为表已成功创建,但也无法在数据库本身中找到它。 当我试图再次发送,并创建它,我得到如下:

> dbSendQuery(redshiftcon, 
     "create table ss_playground.test_table (unique_id VARCHAR, 
category VARCHAR, 
name VARCHAR, 
number_min float);") 

Error in postgresqlExecStatement(conn, statement, ...) : 
RS-DBI driver: (could not Retrieve the result : ERROR: Relation 
'test_table' already exists) 

有什么我失踪?

请帮忙! 谢谢

+1

我不知道,但尝试添加'承诺;'你创建表后。有可能您的更改是特定于会话的,您可能希望先提交它们。 – rohitkulky

回答

2

我认为ss_playground不是此用户/角色的默认架构。您可以将模式设置为默认设置。有一个偷看here

要快速解决您的代码,你可以尝试:

dbExistsTable(redshiftcon, c("ss_playground","test_table")) 

或破解它作为

any(grepl("test_table",dbListTables(redshiftcon))) 
+0

非常感谢您的帮助!用这段代码,我发现实际上这个表存在。 '> dbExistsTable(redshiftcon,c(“ss_playground”,“test_table”)) [1]是否需要后续授予权限? – RCN

+1

假设你的用户名可以访问模式,只需运行SET搜索路径= ss_playground,public;''。所以看起来像''rs < - dbSendQuery(conn = redshiftcon,“SET search_path = ss_playground,public;”)''。那么你不需要做''c(schema,tabl_name)'',并且可以通过''dbExistsTable(redshiftcon,test_table)''来查询。 –

+0

我不特别想公开整个架构,有没有办法只公开这个特定的表?我正在玩'dbSendQuery(redshiftcon,“grant select on test_table in schema ss_playground to readonly;”)'where readonly是另一个用户 – RCN