【问题标题】:Using r sf::st_write to non-public schema in PostgreSQL在 PostgreSQL 中使用 r sf::st_write 到非公共模式
【发布时间】:2019-06-09 16:03:34
【问题描述】:

我正在尝试将空间表写入不是 PostgreSQL 数据库中默认公共架构的架构。

library(sf)
library(DBI)
library(RPostgreSQL)
library(spData)

# PostgreSQL DB parameters
host <- "myHost" 
port <- 5432
username <- "myName"
dbname <- "myDb"
password <- "MyPassword"

# Connect to db  
conn <- dbConnect(PostgreSQL(), dbname = dbname, host = host, port = port, user = username, password = password)

st_write(obj = cycle_hire, dsn = conn, Id(schema="myOtherSchema", table = "myCycle")) # Write data to db - currently only writes to default schema

# Disconnect db
dbDisconnect(conn)

但这会将我的表添加到名为 "myOtherSchema"."myCycle" 的公共架构中。

上面也试过...

dbWriteTable(conn = conn, name = "myCycle", value = cycle_hire, Id(schema="mySchema"))

...替换st_write,这导致myCycle 被写入公共架构。

我做错了什么?

会话信息:

R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server >= 2012 x64 (build 9200)

在 Centos 7 操作系统上运行 PostgreSQL 11.1。

【问题讨论】:

  • 我会尝试使用包RPostgres进行连接:conn
  • @lbusett 没有包RPostgres
  • 您尝试安装了吗(install.packages("RPostgres")?
  • @lbusett 啊哈。看看谷歌搜索给你带来了什么。 OK 运行 conn &lt;- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) 然后 st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) 并得到 Error in st_write.sf(obj = cycle_hire, dsn = conn, Id(schema = "roads_spatial", : no st_write method available for dsn of class list
  • 很高兴它有帮助。只是想用RPostgreSQL 你可以尝试一下: st_write(obj = cycle_hire, dsn = conn, layer = c("myOtherSchema", "myCycle")) (见github.com/r-spatial/sf/issues/557

标签: r postgresql sf


【解决方案1】:

发生这种情况是因为您通过包RPostgreSQL 连接到数据库,但用于指定表和架构的语法与使用包RPostgres 建立的连接使用的语法相同。您可以使用以下方法解决此问题:

    require(RPostgres)
    conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, 
                      user = username, password = password)
    st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle"))

【讨论】:

    【解决方案2】:
    require(RPostgres)
        conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, 
                          user = username, password = password)
        st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle"))
    

    使用 Postgres 12,我在公共场合得到了一个表 "roads_spatial"."mycycle",这不是预期的结果?

    所以一个简单的迂回方式是写给公众,然后使用

    dbExecute(
            conn,
            "ALTER TABLE myCycle SET SCHEMA roads_spatial")
    

     packageVersion("RPostgres") #[1] '1.2.0'
     packageVersion("DBI")       #[1] '1.1.0'
    

    【讨论】:

      【解决方案3】:

      同样使用 Postgres 12,我最初遇到了与 @Cedric 相同的问题(一个名为“roads_spatial.”mycycle”的公共新表。我终于能够成功创建一个名为“mycycle”的新表具有以下内容的“roads_spatial”模式(或我的数据库中的等价物):

      st_write(obj = cycle_hire, dsn = conn, layer = c("roads_spatial", "mycycle"), delete_layer = TRUE)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-05
        • 2020-12-21
        • 2021-12-09
        • 1970-01-01
        • 2021-02-27
        • 2023-01-19
        相关资源
        最近更新 更多