pg_copy_file | R Documentation |
This is a convenience function meant to be used to import data into a locally-run PostgreSQL server. It basically uses PostgreSQL's COPY command to import the data and by default, it automates the creation of the table taking column names from the header (first row) of the file and guessing column types ala ?data.table::fread
pg_copy_file(
con,
file_path,
table,
sep = ";",
nrows = 10000,
header = TRUE,
na.strings = c("", "NA", "NULL"),
colClasses = NULL,
col.names,
encoding = "UTF-8",
...,
unlogged = FALSE,
drop_table = FALSE,
cascade = FALSE,
if_not_exists = FALSE,
create_table = FALSE,
execute = FALSE,
quote = "\b",
escape = quote,
null = "NULL"
)
con |
A database connection. |
file_path |
path to a text file (csv, tab-delimited, etc.) |
table |
The table name, passed on to
|
sep |
The separator between columns. Defaults to the character in the set |
nrows |
The maximum number of rows to read. Unlike |
header |
Does the first data line contain column names? Defaults according to whether every non-empty field on the first data line is type character. If so, or TRUE is supplied, any empty column names are given a default name. |
na.strings |
A character vector of strings which are to be interpreted as |
colClasses |
As in |
col.names |
A vector of optional names for the variables (columns). The default is to use the header column if present or detected, or if not "V" followed by the column number. This is applied after |
encoding |
default is |
... |
Other arguments used by individual methods. |
unlogged |
logical, whether to create an UNLOGGED table |
drop_table |
logical, whether to drop the table before creating it |
cascade |
logical, whether to add CASCADE to the DROP statement |
if_not_exists |
logical, to add IF NOT EXISTS to the query |
create_table |
boolean TRUE if the table should be created. Otherwise, it assumes the table exists |
execute |
logical, whether to execute the query using |
quote |
"quoting character to be used when a data value is quoted" the default value is just a dirty-little-trick to use a value very unlikely to appear |
escape |
"character that should appear before a data character that matches the QUOTE value" |
null |
"Specifies the string that represents a null value." |
This function uses PostgreSQL's COPY FROM filename command. https://www.postgresql.org/docs/current/static/sql-copy.html. According to the documentation, "The file must be accessible to the server and the name must be specified from the viewpoint of the server". Thererfore, this function is intended to be used when you run the PostgreSQL server locally (if you manage to put the file in the server machine, you could also use it, though).
Use UNLOGGED tables and, if possible, create the table and copy command within the same transaction (to improve performance). See some discussion here
TODO: use also the COPY FROM PROGRAM syntax, to import directly from zip files https://www.postgresql.org/docs/current/static/sql-copy.html
TODO: also perhaps use named pipes to read from zip files directly http://www.ralree.com/2009/09/04/reading-compressed-files-with-postgres- using-named-pipes/ https://stackoverflow.com/questions/41738829/importing-zipped-csv-file-into-postgresql
a dplyr reference to the table
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.