Hadley wrote this nice doc on Handling Secrets with R.
You might consider following the advice under "Environment variables".
It suggests you setup a ~/.Renviron file in your base directory (be sure your version control ignores it). Put your host, dbname(s), port, user and password inside it.
E.g.
# .Renviron
CONNECTION_HOST=XXXX.com # not the website i was expecting
CONNECTION_PORT=5432
CONNECTION_DBNAME= serverxxx
CONNECTION_PASSWORD=XXXX
CONNECTION_USR=XXXX
Note that .Renviron is only processed on startup, so you’ll need to restart R to see changes.
And then you can access those values in R with Sys.getenv("CONNECTION_HOST"),
driver<-dbDriver("PostgreSQL")
connection <-dbConnect(
driver,
dbname=Sys.getenv("CONNECTION_DBNAME"),
...
And there are other options there (eg keyring) to be even more secure with your credential information.