Documentation
Search…
Persisting Database States (Postgres)

Persisting Database States (PostgreSQL)

To persist a PostgreSQL database for a learner we will need to create a presave script and a startup script. The presave script will dump the database to a file each time the learner's environment saves. The startup script will load the database from that file. While authoring content we can use the same techniques to create a starting database for the content.

Setting up Persistence for the Learner's Environment

  1. 1.
    Create a new content and select the Postgres stack.
  2. 2.
    Select Interface from the left sidebar
    1. 1.
      Under Tabs Enabled toggle Enable SQL Results Viewer
    2. 2.
      Scroll down and change the database type to PostgreSQL
    3. 3.
      Type in the name of the database you will be using in this content
  3. 3.
    Select Settings in the left sidebar and scroll down
    1. 1.
      Add a Presave Script to dump the database to a file whenever the learner environment is saved: pg_dump {{database}} > ~/workspace/.postgres.sql;
    2. 2.
      Add a Startup Script to load the database from the database dump: psql {{database}} < ~/workspace/.postgres.sql;

Creating a Starting Database for the Content

When authoring content, after creating a database and adding any tables and values that you want the learner to start with, you can run the pg_dump command in a terminal tab (pg_dump {{database}} > ~/workspace/.postgres.sql;) to create a backup. Click Save in the top right corner of the window and the current database state will be restored by the startup script the first time a learner loads the content.