I need to extract data from a BYTEA field of a postgeSQL database. In the BYTEA field, the data is stored in the RDS format (it is the contents of a .rds file created by the command saveRDS corresponding to a dataframe). Using the package RPostgreSQL I am able to retrieve the bytea column in a character object thanks to the code given in this link https://github.com/codeinthehole/rpostgresql/blob/master/RPostgreSQL/tests/bytea.R
But how can I transform then my character object (in RDS format) in the corresponding dataframe ? I try to use readRDS but it seems only to work with a file.
Thank you
## (supposing the connection to the database)
## I do the select statement to retrieve the bytea column
##
rows <- dbGetQuery(con, "SELECT image_bytea from tabdata")
str(rows)
# 'data.frame': 1 obs. of 1 variable:
# $ image_bytea: chr "\\x1f8b08000000000000065| __truncated__
str(rows[1,1])
# chr "\\x1f8b08000000000000065c9d655456585"| __truncated__
test <- postgresqlUnescapeBytea(rows[1,1])
conraw <- rawConnection(test,open="r")
mydf <- readRDS(file=conraw)
# Error in readRDS(file = conraw) : format d'entrée inconnu
When I use the psql in a cmd MSwindows, it works fine:
psql -At -p 5432 -h localhost -d cabri -U postgres -W -c "select lo_from_bytea(0,image_bytea) from tabdata'
# 31385
psql -At -p 5432 -h localhost -d cabri -U postgres -W -c "\lo_export 31385 'c:/upload-postgre/test-out.rds'"
And in R:
mydf <- readRDS(file = "c:/upload-postgre/test-out.rds")
class(mydf)
## [1] "data.frame"
head(mydf)
## tps pc1 pc1f pc1f_partiel
## 1 -0.4000000 154.2554 70.62402 157.4627 93.89868
## 2 -0.3999995 154.2127 70.94351 157.4627 153.9946
## etc...
Laurent