【问题标题】:PostgresqlR/DBI says table does not existPostgresqlR/DBI 说表不存在
【发布时间】:2020-03-21 12:06:07
【问题描述】:

我正在使用 R 3.6.3 和 PostgreSQL 12。我在数据库中有几个表。其中一个有 8,000,000+ 行,我正在尝试将其发送到 R。我正在使用 RPostgreSQL 包,它依赖于 DBI

> library(RPostgreSQL)
Loading required package: DBI
Warning message:
package ‘RPostgreSQL’ was built under R version 3.6.2 
> drv <- dbDriver("PostgreSQL")
> con <- dbConnect(drv, dbname="ebird_work",host="localhost", port=5432, user="postgres")
> dbListTables(con)
[1] "spatial_ref_sys" "grid_sampl"      "gridpt_of_buf"   "or_counties"    
[5] "e_grid"          "ebird_sel_spt"   "ebird_or_cov"    "ebird_dct"      
[9] "ebird_sim"       "fin_pt_no"       "eb_new"          "samp_pt"        
[13] "or_buffered"  

我正在尝试拉入ebird_sel_spt。但是

> dbExistsTable(con, "ebird_sel_spt")
[1] FALSE

所以像下面这样的查询不起作用。

> dbSendQuery(con, "SELECT * FROM ebird_sel_spt LIMIT 1")
Error in postgresqlExecStatement(conn, statement, ...) : 
RS-DBI driver: (could not Retrieve the result : ERROR:  relation "ebird_sel_spt" does not exist
LINE 1: SELECT * FROM ebird_sel_spt LIMIT 1
                  ^
)

来自 PostgreSQL:

SELECT
   *
FROM
   pg_catalog.pg_tables
WHERE
   schemaname != 'pg_catalog'
AND schemaname != 'information_schema';

"p_loc" "ebird_sel_spt" "postgres"      false   false   false   false

我是 PostgreSQL 新手,将它与 R 一起使用。我做错了什么?

编辑 - 在@Parfait 的回答之后: 我尝试将“p_loc”与 .连接器,我仍然得到一个错误。我更新了 R 和 RPostgreSQL,因为一开始有警告说它是在旧版本下构建的。错误仍然存​​在。我还应该寻找什么?

> library(RPostgreSQL)
Loading required package: DBI
> drv <- dbDriver("PostgreSQL")
> con <- dbConnect(drv, dbname="ebird_work",host="localhost", port=5432, user="postgres")
> dbListTables(con)
[1] "spatial_ref_sys" "grid_sampl"      "gridpt_of_buf"   "or_counties"    
[5] "e_grid"          "ebird_sel_spt"   "ebird_or_cov"    "ebird_dct"      
[9] "ebird_sim"       "fin_pt_no"       "eb_new"          "samp_pt"        
[13] "or_buffered"    
> dbExistsTable(con, "ebird_sel_spt")
[1] FALSE
> dbExistsTable(con, "p_loc.ebird_sel_spt")
[1] FALSE
> t <- Id(schema = "p_loc", table = "ebird_sel_spt")
> dbExistsTable(con, t)
[1] FALSE
> con
<PostgreSQLConnection>

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RPostgreSQL_0.6-2 DBI_1.1.0        

loaded via a namespace (and not attached):
[1] compiler_3.6.3

编辑-响应@Parfait 的请求:

#tried a different table to see if the problem was unique to my
#to the table I was interested in. it's not.
> dbExistsTable(con, "p_loc.or_counties")
[1] FALSE

> dbSendQuery(con, "SELECT * FROM p_loc.ebird_sel_spt LIMIT 1")
<PostgreSQLResult>
Warning message:
In postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver warning: (unrecognized PostgreSQL field type geometry (id:29533) in column 18)

我又做了一次 pgtable 查询:

schemename tablename         tableowner tablespace hasindexes hasrules hastriggers rowsecurity
"public"   "spatial_ref_sys" "postgres" [NULL]     true        false      false       false
"p_loc"    "or_counties"     "postgres" [NULL]     true        false  false       false
"p_loc"    "e_grid"          "postgres" [NULL]     true        false  false       false
"p_loc"    "ebird_sel_spt"   "postgres" [NULL]     false       false  false       false

第二次编辑-这是在@Parfait 对他的回答进行的编辑之后。答案似乎有效,我会暂时标记它。

> dbExistsTable(con, c("p_loc", "ebird_sel_spt"))
[1] TRUE
> df <- dbGetQuery(con, "SELECT * FROM p_loc.ebird_sel_spt LIMIT 1")
Warning message:
In postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver warning: (unrecognized PostgreSQL field type geometry (id:29533) in column 18)
> df
GLOBAL_UNIQUE_IDENTIFIER                         COMMON_NAME
URN:CornellLabOfOrnithology:EBIRD:OBS543103161 Band-tailed Pigeon
SCIENTIFIC_NAME         OBSERVATION_COUNT STATE_PROVINCE     COUNTY LOCALITY
Patagioenas fasciata                  1 Oregon             Washington   CRNA 5
LONGITUDE LATITUDE OBSERVATION_DATE TIME_OBSERVATIONS_STARTED
-123.0843  45.4498       2012-06-03                  06:22:00
FIRST_NAME                              LAST_NAME PROTOCOL_TYPE
Metro Parks and Nature Avian Monitoring      Weil    Stationary
ALL_SPECIES_REPORTED SAMPLING_EVENT_IDENTIFIER            OBS_TIME  point
                   1                 S40033350 2012-06-03 06:22:00 120747
                                             geom   pt_lon  pt_lat
01010000201E690000F851624162191E41A6F66655B8325341 493144.6 5032673

【问题讨论】:

  • 您正在重复您最初尝试的查询,但没有限定架构名称 p_loc. in dbSendQuery
  • @Parfait,抱歉查询错误。我编辑了问题以反映正确的查询。但现在有一个错误我不明白。
  • 避免使用SELECT *,但明确定义列SELECT Col1, Col2, Col3, ...。错误似乎表明第 18 列是问题所在。但这可能是另一个问题。您可能正在使用 PostGIS?另外,看看下面现在是否有效,以便我们可以关闭它。谢谢。
  • @Parfait,是的,我也在使用 PostGIS。感谢您的所有努力!

标签: r postgresql


【解决方案1】:

由于您的 pg_catalog.pg_tables 查询显示您省略了重要的列标题,因此该表存在于非默认架构(即非public 架构)中,特别是在 p_loc 中:

| schemaname | tablename     | tableowner | tablespace | hasindexes | hasrule | hastriggers |
|------------|---------------|------------|------------|------------|---------|-------------|
| p_loc      | ebird_sel_spt | postgres   | NULL       | false      | false   | false       |

因此,请考虑在所需的方法调用中识别模式或在 SQL 中使用句点限定符以下模式:database.schema.table.column

# SCHEMA NAMESPACE
dbExistsTable(con, c("p_loc", "ebird_sel_spt"))
df <- dbReadTable(con, c("p_loc", "ebird_sel_spt"))

# SQL PERIOD-QUALIFIER
df <- dbGetQuery(con, "SELECT * FROM p_loc.ebird_sel_spt LIMIT 1")

【讨论】:

  • 感谢您的回答。我试过了,还是不行。
  • SELECT * FROM p_loc.ebird_sel_spt LIMIT 1 怎么样?请发布您的 Postgres pg_tables 查询的列。
  • 根据您的编辑,您没有对架构名称进行质量评估。请在表名上使用p_loc. 准确运行上述查询。另外,不管dbExistsTable,我在这里发帖的dbReadTable 有效吗?
  • 我编辑解决方案以避免使用Id(),而是直接在方法调用中传递架构信息。
猜你喜欢
  • 2011-09-06
  • 2016-12-14
  • 2011-01-22
  • 2022-01-13
  • 1970-01-01
  • 2022-11-22
  • 2012-04-23
  • 2021-09-20
相关资源
最近更新 更多