diff --git a/docs/safe-haven-services/accessing-databases.md b/docs/safe-haven-services/accessing-databases.md new file mode 100644 index 000000000..3beb69223 --- /dev/null +++ b/docs/safe-haven-services/accessing-databases.md @@ -0,0 +1,50 @@ +# Accessing Databases + +Some data in Safe Havens may be accessible via a database rather than files. + +If you have a project with database access, your Research Coordinator will arrange for your accounts to be created on the database. Once your account is created, you will receive the `database name`, `host` and `port`, which you can use to connect programmatically (i.e., via R, Python, or any other language). + +## R + +To connect to a Safe Haven database via R, follow the [official documentation](https://solutions.posit.co/connections/db/getting-started/connect-to-database/) on database connections. The required connection packages will depend on the [type of database](https://solutions.posit.co/connections/db/databases/). + +Example connection to a PostgreSQL database via [RPostgres](https://solutions.posit.co/connections/db/databases/postgresql/#using-the-rpostgres-package): + +```r +library(DBI) +library(RPostgres) +con <- dbConnect( + RPostgres::Postgres(), + dbname = "", + host="", + port="", + user = .rs.askForPassword("Enter Username:"), + password = .rs.askForPassword("Enter Password:") +) +``` + +## Python + +Python has many database connection packages depending on the type of database. We recommend you choose a highly supported package and follow the package's official documentation. + +Example connection to a PostgreSQL database via [psycopg](https://www.psycopg.org/docs/index.html): + +1. Install psycopg2 + + ```console + $ pip install psycopg2 + ``` + +1. Connect + + ```python + import psycopg2 + + con = psycopg2.connect( + database = "", + host="", + port="", + user=input("Enter Username:"), + password=input("Enter Password:") + ) + ``` diff --git a/mkdocs.yml b/mkdocs.yml index 05b7140b4..42a2c8052 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -90,6 +90,7 @@ nav: - "Network Access Controls": safe-haven-services/network-access-controls.md - "Safe Haven Access": safe-haven-services/safe-haven-access.md - "Virtual Desktop Connections": safe-haven-services/virtual-desktop-connections.md + - "Accessing Databases": safe-haven-services/accessing-databases.md - "Using the HPC Cluster": safe-haven-services/using-the-hpc-cluster.md - "Superdome Flex Tutorial": - "Accessing the SDF Inside the EPCC TRE": safe-haven-services/superdome-flex-tutorial/L1_Accessing_the_SDF_Inside_the_EPCC_TRE.md