Skip to content

Instantly share code, notes, and snippets.

@dpprdan
Last active January 17, 2022 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpprdan/909d3d6221cbd92bd1da0b5178829275 to your computer and use it in GitHub Desktop.
Save dpprdan/909d3d6221cbd92bd1da0b5178829275 to your computer and use it in GitHub Desktop.
Setup Docker container for testing r-dbi packages

How to install and run a Docker container with MariaDB in order to test RMariaDB.

Install and start Docker.

Pull the MariaDB docker image

docker pull mariadb

Start the MariaDB Docker container. Note that the data created in this MariaDB 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 rmariadbtest -d --rm -p 3306:3306 -e MARIADB_ROOT_PASSWORD=mysecretpassword -e MARIADB_DATABASE=test mariadb

Check whether the MariaDB container started and is still running

docker ps

Now create a MariaDB configuration file and add the following:

[rs-dbi]
database="test"
user="root"
password="mysecretpassword"

Store this file as ~/.my.cnf on Linux, Unix, or Mac OS X (i.e. inside your home directory). On Windows store it as C:\my.cnf, i.e. in the root of your C drive. For other possible locations see Configuring MariaDB with Option Files.

Now you should be all set, i.e. RMariaDB::MariaDBHasDefault() should return TRUE.

When you are done testing, you can stop the MariaDB container like so:

docker stop rmariadbtest

To access the container via Bash, you can run this command:

docker exec -it rmariadbtest bash

How to install and run a Docker container with PostgreSQL in order to test RPostgres.

Install and start Docker.

Pull the Postgres docker image

docker pull postgres

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

docker ps

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.

PGUSER=postgres

Now you should be all set, i.e. RPostgres::postgresHasDefault() should return TRUE.

When you are done testing, you can:

Stop the Postgres container

docker stop pg-rpostgres

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment