How to install and run a Docker container with PostgreSQL in order to test RPostgres.
Install and start Docker.
Pull the Postgres docker image
Start the Postgres Docker container
(note that the data created in this Postgres container is not persistent, i.e. it will be gone once you stop the container)
Choose your own password instead of mysecretpassword.
docker run --name pg-rpostgres -d --rm -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword postgres
Check whether the Postgres container started and is still running
Store your password in the .pgpass password file
The file .pgpass in a user's home directory or the file referenced by PGPASSFILE can contain passwords to be used if the connection requires a password (and no password has been specified otherwise). On Microsoft Windows the file is named %APPDATA%\postgresql\pgpass.conf (where %APPDATA% refers to the Application Data subdirectory in the user's profile).
https://www.postgresql.org/docs/current/libpq-pgpass.html
The advantage of storing the password in the .pgpass file is that the password can then be used by every application that uses the libpq library, e.g. psql (see below), not only RPostgres from within R.
Alternatively you can set the password in RPostgres as an environment variable (see below).
# hostname:port:database:username:password
localhost:5432:postgres:postgres:mysecretpassword
Set the postgres user name as an environment variable in R
By default, RPostgres will try to connect to the database with OS user name (see argument user in DBI::dbConnect()), unless you set the PGUSER environment variable (see argument user in RPostgres::dbConnect()).
To set the environment in your current R session only, do:
Sys.setenv(PGUSER = "postgres")
You can set the PGPASSWORD environment variable in the same way, if you do not want to use the .pgpass file mentioned above.
You can also store the environment variables in .Renviron, so it will be loaded everytime you start R. You have to restart R or run readRenviron("~/.Renviron") in order to load the environment variables you just added to the .Renviron file.
Now you should be all set, i.e. RPostgres::postgresHasDefault() should return TRUE.
When you are done testing, you can:
Stop the Postgres container
Extras
Sometimes it is necessary to connect to compare output from RPostgres with direct Postgres output, or rather psql.
You can run psql directly within the Postgres Docker container like so:
docker exec -it -u postgres pg-rpostgres psql
If you have psql installed locally and it is on the PATH, you can connect to the Postgres DB in Docker like this:
psql -h localhost -U postgres -d postgres
You will get a password prompt unless you saved the password in .pgpass.