我正在使用R 3.6.3和PostgreSQL 12。我在数据库中有几个表。其中一个有8000000多行,我正试图将其发送到R。我使用的是依赖于DBI
的RPostgreSQL
包。
> 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
当您的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")