I'm trying to use the large object (https://www.postgresql.org/docs/10/largeobjects.html) feature of PostgreSQL in R, and I have some trouble writing and reading using {DBI}/{RPostgres}.
Here is what I have tried so far:
# Getting the db
docker run --rm --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5433:5432 postgres
library(DBI)
con <- dbConnect(
RPostgres::Postgres(),
dbname = "postgres",
host = "localhost",
port = 5433,
user = "postgres",
password = "mysecretpassword"
)
Creation works :
> dbGetQuery(con, "SELECT lo_create(1234);")
lo_create
1 1234
But then I have a hard time figuring out how to write an R object to this large object.
For example, how would I write mtcars as a large object in Postgres using {DBI} and {RPostgres}?
And then, how do I read it back again in R?
Consider R's
serialize()(the underlying build of .RData/.RDS formats) to save R objects into a PostgresOIDcolumn for large objects and use Postgres v10+ server-side large object functions to create and retrieve content. Below can possibly work withbyteatypes by removing alllo_*functions.Assuming table structure:
To append the R object:
To retrieve the R object:
Alternatively, you can use Postgres'
TEXTtype with unlimited length and R'sdput(ASCII representation of R objects) witheval+parseon returneddputstring.