title: How to write a table in PostgreSQL from R?
body: <p>At present to insert data in a PostgreSQL table I have to create an empty table and then do an <code>insert into table values ...</code> along with a dataframe collapsed insto a single string with all the values. It doesn't work for large sized dataframes.</p>
  
  <p>The <code>dbWtriteTable()</code> doesn't work for PostgreSQL and gives the following error...</p>
  
  <pre><code>Error in postgresqlpqExec(new.con, sql4) : RS-DBI driver: (could not Retrieve the result : ERROR: syntax error at or near "STDIN" LINE 1: COPY "table_1" FROM STDIN
  </code></pre>
  
  <p>I have tried the following hack as suggested in answer to a similar question asked before. Here's the link... <a href="https://stackoverflow.com/questions/26449666/how-do-i-write-data-from-r-to-postgresql-tables-with-an-autoincrementing-primary/26451425?noredirect=1#comment62757555_26451425">How do I write data from R to PostgreSQL tables with an autoincrementing primary key?</a></p>
  
  <pre><code>body_lines &lt;- deparse(body(RPostgreSQL::postgresqlWriteTable))
  new_body_lines &lt;- sub(
    'postgresqlTableRef(name), "FROM STDIN")', 
    'postgresqlTableRef(name), "(", paste(shQuote(names(value)), collapse = ","), ") FROM STDIN")', 
    body_lines,
    fixed = TRUE
  )
  fn &lt;- RPostgreSQL::postgresqlWriteTable
  body(fn) &lt;- parse(text = new_body_lines)
  while("RPostgreSQL" %in% search()) detach("package:RPostgreSQL")
  assignInNamespace("postgresqlWriteTable", fn, "RPostgreSQL")
  </code></pre>
  
  <p>This hack still doesn't work for me. The <code>postgresqlWriteTable()</code> throws exactly the same error...
  What exactly is the problem here?</p>
  
  <p>As an alternative I have tried using <code>dbWriteTable2()</code> from <code>caroline</code> package. And it throws a different error...</p>
  
  <pre><code>Error in postgresqlExecStatement(conn, statement, ...) : 
    RS-DBI driver: (could not Retrieve the result : ERROR:  column "id" does not exist in table_1
  )
  creating NAs/NULLs for for fields of table that are missing in your df
  Error in postgresqlExecStatement(conn, statement, ...) : 
    RS-DBI driver: (could not Retrieve the result : ERROR:  column "id" does not exist in table_1
  )
  </code></pre>
  
  <p>Is there any other method to write a large dataframe into a table in PostgreSQL directly?</p>