很长一段时间以来,我一直试图解决RPostgreSQL包的问题。我的代码:
path_ene <- "C:/Users/omen03/ENE"
nene <- "ene_2010_02_efm.csv"
drv <- dbDriver("PostgreSQL")
con = dbConnect(drv, user="postgres", password="mypassword",
host="localhost", port=5432, dbname="ENE")
tn = "ene_2010_02_efm";
dbRemoveTable(con,tn);
dbWriteTable(conn = con, name = tn, value = paste0(path_ene,"/",nene),
sep = ",", overwrite = FALSE))在执行上面描述的代码时,我抛出以下错误:
postgresqlExecStatement中的错误(conn,语句,.):(无法检索结果:错误:无法打开文件C:/postgresqlExecStatement/RS 03/ene/ene_2010_02_efm.csv)用于读取:权限被拒绝的
提示:“复制自”通知PostgreSQL服务器进程读取文件。您可能需要使用客户端工具,如psql \ copy。)此外:警告消息:在postgresqlImportFile (conn,name,value,.):无法将数据加载到表中
当我尝试不指定文件路径时,它会抛出另一个错误。
dbRemoveTable(con,tn);
dbWriteTable(conn = con, name = tn, value = nene),
sep = ",", overwrite = FALSE))postgresqlExecStatement中的错误(conn,语句,.):(无法检索结果:错误:无法打开文件./ ene_2010_02_efm»以便读取:没有这样的文件或目录)
提示:“复制自”通知PostgreSQL服务器进程读取文件。您可能需要使用客户端工具,如psql \ copy。)此外:警告消息:在postgresqlImportFile (conn,name,value,.):无法将数据加载到表中
更新:
考虑到错误消息,我决定直接使用sql在数据库中的每个表中插入csv值。为此,我使用了几个函数
createEmptyTable <- function(con,tn,df) {
sql <- paste0("create table \"",tn,"\" (",paste0(collapse=',','"',names(df),'" ',sapply(df[0,],postgresqlDataType)),");");
dbSendQuery(con,sql);
invisible();
};摘自:How to write a table in PostgreSQL from R?
和
#tn: Table name
#c_names: column names of each table (a list)
#source: The path to each csv files
insert_data = function(tn, source){
sql = paste0('COPY ',tn,' FROM \'',paste0(path_ene,'\\',source),'\' DELIMITER \',\' CSV HEADER')
dbSendQuery(con, sql);
}
insert_data(tn[1], paste0(path_ene, "/",nene[1]))不管怎么说,我还是有一个非常相似的错误。
postgresqlExecStatement中的错误(conn,语句,.):(无法检索结果:错误:无法打开文件C:/User/02 03/ene/ene_2010_02_efm.csv)用于读取:权限拒绝提示:从PostgreSQL服务器进程读取文件的副本。您可能需要使用客户端工具,如psql \ copy。)
我的会议信息:
R版本3.6.1 (2019-07-05)平台:x86_64-W64-mingw32 32/ x64 (64位),运行于: Windows 10 x64(构建18362)
矩阵产品:默认
区域设置:1个LC_COLLATE=Spanish_Chile.1252 LC_CTYPE=Spanish_Chile.1252 LC_MONETARY=Spanish_Chile.1252 LC_NUMERIC=C 5 LC_TIME=Spanish_Chile.1252附加基本包:1个stats图形grDevices实用程序
数据集方法基于其他附加包:1 RPostgreSQL_0.6-2 DBI_1.0.0
进口0.8-71 captioner_2.2.3 kableExtra_1.1.0 wordcloud_2.6 tidytext_0.2.2 gridExtra_2.3 9 gtable_0.3.0
readstata13_0.9.2 ggrepel_0.8.1 pbapply_1.1-2 ggrepel_0.8.1 pbapply_1.4-2 srvyr_0.3.6 data.table_1.12.6 lubridate_1.7.4 17 stringi_1.4.3 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3
通过命名空间加载的purrr_0.3.3 readr_1.3.1 tidyr_1.0.0 tibble_2.1.3 25 ggplot2_3.2.1 tidyverse_1.3.0 (未附加):1 httr_1.4.1
jsonlite_1.6 viridisLite_0.3.0 splines_3.6.1 modelr_0.1.5
assertthat_0.2.1 cellranger_1.1.0 sessioninfo_1.1.1 9 pillar_1.4.3 backports_1.1.5晶格0.20-38 glue_1.3.1
digest_0.6.23 rvest_0.3.5颜色空间_1.4-1 htmltools_0.4.0 17矩阵_1.2-17 survey_3.36 pkgconfig_2.0.3 broom_0.5.2 haven_2.2.0 scales_1.1.0 webshot_0.5.2 generics_0.0.2
25 withr_2.1.2 lazyeval_0.2.2 cli_2.0.0
生存2.44-1.1 magrittr_1.5 crayon_1.3.4 readxl_1.3.1
evaluate_0.14 33 tokenizers_0.2.1 janeaustenr_0.1.5 fs_1.3.1
fansi_0.4.0 nlme_3.1-140 SnowballC_0.6.0 xml2_1.2.2
tools_3.6.1 41 hms_0.5.2 mitools_2.4
lifecycle_0.1.0 munsell_0.5.0 reprex_0.3.0 compiler_3.6.1
rlang_0.4.2 grid_3.6.1 49 rstudioapi_0.10
rmarkdown_1.18 R6_2.4.1 knitr_1.26 zeallot_0.1.0
parallel_3.6.1 Rcpp_1.0.3 vctrs_0.2.1 57 dbplyr_1.4.2 tidyselect_0.2.5 xfun_0.11
发布于 2020-02-17 02:38:18
最后,我通过寻找将数据直接导入postgresql的问题找到了答案。更改包含.csv文件的文件夹的配置就足够了。
我复制了这个answer,这是我最后使用的那个。
https://stackoverflow.com/questions/60245537
复制相似问题