PostgresqlR/DBI说表不存在
我正在使用R3.6.3和PostgreSQL 12。我在数据库中有几个表。其中一个有8000000多行,我正在尝试将其发送到R。我正在使用RPostgreSQL包,它依赖于DBI 所以像下面这样的查询不起作用PostgresqlR/DBI说表不存在,r,postgresql,R,Postgresql,我正在使用R3.6.3和PostgreSQL 12。我在数据库中有几个表。其中一个有8000000多行,我正在尝试将其发送到R。我正在使用RPostgreSQL包,它依赖于DBI 所以像下面这样的查询不起作用 > dbSendQuery(con, "SELECT * FROM ebird_sel_spt LIMIT 1") Error in postgresqlExecStatement(conn, statement, ...) : RS-DBI driver: (could not
> 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对他的答案进行编辑之后。答案似乎是有效的,我会马上给它打分
> 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中:
因此,考虑在需要的方法调用中识别模式,或者在SQL以下模式中使用句点限定符:Dava.Schema.Tab.Stult:
请参阅以下已接受的答案:您正在重复您最初尝试的查询,但没有限定架构名称p_loc。在dbSendQuery中。@Parfait,很抱歉进行了错误的查询。我编辑了这个问题以反映正确的问题。。但现在我不明白有一个错误。请避免使用SELECT*,而是显式定义列SELECT Col1、Col2、Col3。。。。错误似乎表明第18列是问题所在。但这可能是另一个问题。你可能在用PostGIS?另外,看看下面的方法现在是否有效,这样我们就可以解决这个问题了。谢谢。@Parfait,是的,我也在用PostGIS。谢谢你的努力!谢谢你的回答。我试过了,但没用。从p_loc.ebird_sel_spt LIMIT 1选择*怎么样?请发布Postgres pg_tables查询的列。从您的编辑中,您没有对架构名称进行质量检查。请使用p_loc准确运行上述查询。在表名上。另外,不管dbExistsTable如何,在这里发布dbReadTable是否有效?我编辑解决方案以避免使用Id,但直接在方法调用中传递模式信息。
> 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
#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)
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
> 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
| schemaname | tablename | tableowner | tablespace | hasindexes | hasrule | hastriggers |
|------------|---------------|------------|------------|------------|---------|-------------|
| p_loc | ebird_sel_spt | postgres | NULL | false | false | false |
# 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")