I have been trying to push data created in a work space on R server back to Redshift. Using dbWriteTable(), I can successfully create the table, but the data are not being written to this newly created table and the following error is produced:
> dbWriteTable(con, c("schema", "table"), value = df,append=TRUE,
row.names=FALSE)
Error in postgresqlpqExec(new.con, sql4) :
RS-DBI driver: (could not Retrieve the result : ERROR: syntax error at or
near "STDIN"
LINE 1: COPY "schema"."table" FROM STDIN
^
)
Notes on the code:
'con' refers to the Redshift connection, the code for that in generic form is as follows:
con <- dbConnect(drv, host="host_name",
port="port_id",
dbname="db_name",
user="username",
password="password")
I cannot give full examples with data due to restrictions on the data.
Originally, I thought it could be a permissions issue within the work space. But, I have found an extremely slow solution by creating the table using dbSendQuery() and then looping through each row of the dataframe I wish to insert into Redshift via another call to dbSendQuery(). This tells me the issue must be related to something other then permissions since I can complete the task inefficiently.
Any comments or proposals related to this error are greatly appreciated, thank you.
It looks like your client "R" application is trying to use Postgres COPY ... FROM STDIN syntax. "FROM STDIN" is not supported syntax for the Redshift COPY command, see Redshift COPY.
Options you might consider are inserting row by row (as you were doing with dbSendQuery()), but this will be slow, or outputting your data to a CSV file that can be uploaded to Amazon S3 and from there loaded using COPY with S3 as a data source (fast).