Skip to content

Instantly share code, notes, and snippets.

@zross
Created March 30, 2017 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zross/89a2c750bed93c8201723593d6bb0dea to your computer and use it in GitHub Desktop.
Save zross/89a2c750bed93c8201723593d6bb0dea to your computer and use it in GitHub Desktop.
Read and write from R to postgresql/postgis
library(sf)
library(RPostgreSQL)
dat <- data.frame(ID = 1:2,
long = c(-74.003234, -73.983317),
lat = c(40.731863, 40.722684))
dat_sf <- st_as_sf(dat, coords = c("long", "lat"),
crs = 4326,
agr = "identity")
con <- dbConnect(PostgreSQL(), dbname = "mydb", host = "localhost")
st_write_db(con, dat_sf, "mydata")
dbListTables(con) # [1] "spatial_ref_sys" "mydata"
new_dat <- st_read_db(con, "mydata")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment