From 62d766c62e4d8e39f75b0e1ad3588c09e394ff3c Mon Sep 17 00:00:00 2001 From: bprodan Date: Fri, 5 Jul 2024 11:22:15 +0100 Subject: [PATCH 1/2] SHS - adding doc on db access --- .../accessing-databases.md | 50 +++++++++++++++++++ mkdocs.yml | 1 + 2 files changed, 51 insertions(+) create mode 100644 docs/safe-haven-services/accessing-databases.md diff --git a/docs/safe-haven-services/accessing-databases.md b/docs/safe-haven-services/accessing-databases.md new file mode 100644 index 000000000..c34bea244 --- /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 + ``` + +2. 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 From fc37a3ea8f59b937a4908cf8b61a274402627104 Mon Sep 17 00:00:00 2001 From: bprodan Date: Fri, 5 Jul 2024 11:47:58 +0100 Subject: [PATCH 2/2] Fix OL --- docs/safe-haven-services/accessing-databases.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/safe-haven-services/accessing-databases.md b/docs/safe-haven-services/accessing-databases.md index c34bea244..3beb69223 100644 --- a/docs/safe-haven-services/accessing-databases.md +++ b/docs/safe-haven-services/accessing-databases.md @@ -35,7 +35,7 @@ Example connection to a PostgreSQL database via [psycopg](https://www.psycopg.or $ pip install psycopg2 ``` -2. Connect +1. Connect ```python import psycopg2