r-在RPostgreSQL中使用dbWriteTable写入表的权限被拒绝



很长一段时间以来,我一直在尝试解决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,statement,…):RS-DBI驱动程序:(无法检索结果:错误:无法打开文件«C:\Users/omen03/ENE/ENE_2010_02_efm.csv»进行读取:权限被拒绝

提示:COPY FROM告诉PostgreSQL服务器进程读取文件您可能需要使用客户端工具,如psql\copy。)此外:警告消息:在postgresqlImportFile(conn,name,值,…):无法将数据加载到表中

当我尝试不指定文件路径时,它会引发另一个错误

dbRemoveTable(con,tn);
dbWriteTable(conn = con, name = tn, value = nene),
sep = ",", overwrite = FALSE))

postgresqlExecStatement中的错误(conn,statement,…):RS-DBI驱动程序:(无法检索结果:错误:无法打开文件«。/ene_2010_02_efm»用于读取:没有这样的文件或目录

提示:COPY FROM告诉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();
};

摘自:如何从R在PostgreSQL中编写表?

#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,statement,…):RS-DBI驱动程序:(无法检索结果:错误:无法打开文件«C:\/Users/omen03/ENE/ENE_2010_02_efm.csv»用于阅读:权限拒绝提示:COPY FROM告诉PostgreSQL服务器进程读取文件您可能需要使用客户端工具,如psql\copy。)

我的会话信息:

R版本3.6.1(2019-07-05)平台:x86_64-w64-mingw32/x64(64位)运行于:Windows 10 x64(内部版本18362)

矩阵产品:默认

区域设置:[1]LC_COLLATE=Spanish_Chile.152LC_CTYPE=Spanish_Chile.1252LC_MONETARY=Spanish_Chile.1252 LC_NUMERIC=C[5]LC_TIME=西班牙_智利.1252

附加的基本包:[1]统计图形grDevices实用程序
数据集方法基本

其他附加包:[1]RPostgreSQL_0.6-2 DBI_1.0.0
foreign_0.8-71 captiononer2.2.3 kableExtra_1.1.0 wordcloud_2.6tidytext_0.2.2 gridExtra_2.3[9]gtable_0.3.0
readstata13_0.9.2 RColorBrewer_1.1-2 ggrejection_0.8.1 pbapply_1.4-2srvyr_0.3.6数据。表_1.12.6 lubridate_1.7.4[17]字符串_1.4.3用于字符串_0.4.0字符串_1.4.0 dplyr_08.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_03.0 splines_3.6.1 modelr_0.1.5
asserthat_0.2.1 cellranger_1.1.0 sessioninfo_1.1.1[9]支柱_1.4.3后门_1.1.5格子_0.20-38胶水_1.3.1
摘要_0.6.23服务器_0.3.5颜色空间_1.4-1 htmltools_0.4.0[17] 矩阵_1.2-17调查_3.36 pkgconfig_2.0.3扫帚_0.5.2haven2.2.0 scales_1.1.0 webshot_0.5.2 generics_0.0.2
[25]with r_2.1.2 lazyval_0.2 cli_2.0.0
survival_2.44-1.1 magrittr_1.5 crane_1.3.4 readxl_1.3.1
evaluate_0.14[33]tokenizers_0.2.1 janeaustenr_0.1.5 fs_1.3.1
fansi0.4.0 nlme_3.1-140 SnowballC_0.6.0 xml2_1.2.2
tools_3.6.1[41]hms_05.2mitools_2.4
生命周期-0.1.0 munsell_05.0 reprex_03.0 compiler_3.6.1
rlang_0.4.2 grid_3.6[49]rstudioapi_0.10
rmarkdown _1.18 R6_2.4.1 knifer_1.26 zeallot_0.1.0
parallel_36.1 Rcpp_1.0.3 vctrs_0.2.1[57]dbply_1.4.2 tidyselect_0.2.5 xfun_0.11

最后,我通过查找将数据直接导入postgresql的问题找到了答案。只需更改包含.csv文件的文件夹的配置即可。

我复制了这个答案,这就是我最终使用的答案。

  • 右键单击包含被拒绝访问权限的数据文件的文件夹,然后单击"属性">
  • 在文件夹的属性窗口中,选择安全选项卡
  • 单击"编辑"按钮。在打开的"文件夹权限"窗口中,单击添加。。。按钮
  • 在"输入要选择的对象名称"文本区域框中键入Everyone
  • 单击"确定",窗口将关闭

最新更新