首页 > 解决方案 > R连接到postgres在dbExistsTable中返回false,但它是错误的

问题描述

我尝试在 postgres 中连接到我的表。这是我的数据库的屏幕。 在此处输入图像描述

所以我需要 dbo.social 和表配置文件。所以要做到这一点,我尝试

library(RPostgres)
library(DBI)

pw<- {
  "1234"
}

con <- dbConnect(RPostgres::Postgres()
                 , host='1.2.3.4.'
                 , port='5432'
                 , dbname='social'
                 , user='analyst'
                 , password=pw)


#rm(pw) # removes the password

dbExistsTable(con, "social")

和结果

[1] FALSE

为什么是假的,我标记为它存在的黑线。

所以

> dbListTables(con)
 [1] "cache"                         "available_permission_modules" 
 [3] "available_permissions"         "counters"                     
 [5] "permissions"                   "group_direction"              
 [7] "jobs"                          "oauth_auth_codes"             
 [9] "oauth_access_tokens"           "oauth_refresh_tokens"         
[11] "permissions_rules"             "permissions_rule_user"        
[13] "oauth_clients"                 "oauth_personal_access_clients"
[15] "users"                         "directions"                   
[17] "themes"                        "profiles_without_rating"      
[19] "failed_jobs"                   "model_has_permissions"        
[21] "regions_oktmo"                 "ch_profiles"                  
[23] "user_reports"                  "roles"                        
[25] "migrations"                    "crime_minor"                  
[27] "governments"                   "mapping_candidates"           
[29] "password_resets"               "responsible"                  
[31] "spatial_ref_sys"               "model_has_roles"              
[33] "population"                    "role_has_permissions"         
[35] "geo_point"                     "geo_polygon"                  
[37] "crime_all"                     "geography_columns"            
[39] "geometry_columns"              "raster_columns"               
[41] "raster_overviews"              "schools"                      
[43] "post_grabber"                 

为什么列表中没有 social.profiles_bstms?

我怎样才能得到 social.profiles_bstms 表来使用它。

标签: rpostgresqlrodbcrpostgresqlrpostgres

解决方案


正如 AEF 和 Data Miner 的直接回复中所提到的,您要做的是验证profile_bstms架构内是否存在表social(并且此架构位于同名的数据库内)。

请注意 Postgres Schemas [ref.1] 本身不包含数据;它们用于组织您的表格。

我以前从未使用过 R,但我发现了一些可以带来一些亮点的链接。

  1. https://github.com/r-dbi/RPostgres/issues/160
  2. https://github.com/r-dbi/DBI/issues/277

关于您为什么dbListTables(con)不显示这些表的问题:我会说您正在观察的列表来自 schema 上存在的表,public对吗?

如果是这种情况,这是因为参数search_path不包含您要列出的模式,在这个例子中social

我的建议是请您尝试更改search_path参数。类似的东西:"$user", public, social。它不需要重新启动数据库。

[1] https://www.postgresql.org/docs/current/ddl-schemas.html


推荐阅读