2.2 Run the database locally
On Github we provide a docker image of the Open Discourse database. You can use this docker image to run the database locally on your machine. The docker image contains the latest version of the Postgres database. This section guides you through the steps necessary to run the database on your computer.
2.2.1 Get and launch the database
Three steps are necessary to run the database on your machine:
Authenticate to GitHub Container Registry
At first you need a GitHub account and authenticate with a personal access token to the
GitHub Container Registry
. This process just involves some quick steps. Click here for a detailed documentation on how to get and use your personal access token. You can follow the three steps described under “Authenticating with the container registry” and you are done.Pull the docker container
After you are authenticated run the following command from your terminal:
docker pull ghcr.io/open-discourse/open-discourse/database:latest
Run the database
Finally you just need to start the docker container with the database:
docker run --env POSTGRES_USER=postgres --env POSTGRES_DB=postgres --env POSTGRES_PASSWORD=postgres -p 5432:5432 -d ghcr.io/open-discourse/open-discourse/database
The database is running on localhost:5432 now. See the table below for details on the environment variables passed on to the docker run
command.
variable | value |
---|---|
–env POSTGRES_USER | postgres |
–env POSTGRES_DB | postgres |
–env POSTGRES_PASSWORD | postgres |
2.2.2 Use the database
After these steps are done, the database is running locally on port 5432
. You can access the database by using a database tool (e.g. DBeaver) or by using a programming language.
Use following credentials to log in to your database:
variable | value |
---|---|
db_name | next |
host_db | localhost |
db_port | 5432 |
db_user | postgres |
db_password | postgres |
2.2.3 Code examples
The following code examples in Python
and R
show how to access a locally running Open Discourse database.
In both examples the Speeches Table is retrieved.
Python Code
import psycopg2
import pandas as pd
# db_connection -----------------------------------------------------------
= {
con_details "host" : "localhost",
"database" : "next",
"user" : "postgres",
"password" : "postgres",
"port" : "5432"
}= psycopg2.connect(**con_details)
con
# get data tables ---------------------------------------------------------
= pd.read_sql_query("select * from open_discourse.speeches", con) speeches
R Code
library("RPostgreSQL")
# db_connection -----------------------------------------------------------
<- "next"
db <- "localhost"
host_db <- "5432"
db_port <- "postgres"
db_user <- "postgres"
db_password <-
con dbConnect(
::Postgres(),
RPostgresdbname = db,
host = host_db,
port = db_port,
user = db_user,
password = db_password
)
# get data tables ---------------------------------------------------------
<- dbGetQuery(con,
speeches "SELECT *
FROM open_discourse.speeches;")