As Craig Ringer said, setting client_encoding
to windows-1252 is probably not the best thing to do. Indeed, if the data you're retrieving contains a single exotic character, you're in trouble:
Error in postgresqlExecStatement(conn, statement, ...) : RS-DBI
driver: (could not Retrieve the result : ERROR: character 0xcca7 of
encoding "UTF8" has no equivalent in "WIN1252" )
On the other hand, getting your R environment to use Unicode could be impossible (I have the same problem as you with Sys.setlocale
... Same in this question too.).
A workaround is to manually declare UTF-8 encoding on all your data, using a function like this one:
set_utf8 <- function(x) {
# Declare UTF-8 encoding on all character columns:
chr <- sapply(x, is.character)
x[, chr] <- lapply(x[, chr, drop = FALSE], `Encoding<-`, "UTF-8")
# Same on column names:
Encoding(names(x)) <- "UTF-8"
x
}
And you have to use this function in all your queries:
set_utf8(dbGetQuery(con, "SELECT myvar FROM mytable"))
EDIT: Another possibility is to use RPostgres unstead of RPostgreSQL. I tested it (with the same config as in your question), and as far as I can see all declared encodings are automatically set to UTF-8.