A postgresql database for storage of notable quotes and notes.
Author: Jess Moore
License: MIT
Data is persisted on the host in folder ./pgdata (not version controlled).
If the container and ./pgdata are deleted and the repo is recloned, then the method to install the data is to restore the data from db dump file ~/quotes-[LAST_DATE].sql.gz
Start docker container using quote database data in ./pgdata.
docker compose up --detachConfirm by inspecting database in container using:
docker compose exec -it [POSTGRES_SERVICE_NAME] psql -U [POSTGRES_USER] -d [POSTGRES_DB]
\dt
\d quotes
select author from quotes;
\qor from within container via Docker Desktop:
psql -U [POSTGRES_USER] -d [POSTGRES_DB]
select author from quotes;Scripts are installed to $HOME/bin with:
chmod u+x db/quote.sh
chmod u+x db/get_quote.sh
ln -s db/quote.sh ~/bin/quote
ln -s db/get_quote.sh ~/bin/get_quoteAdd new quote. New quotes are added using quote new ... to create the new quote record and quote update ... [id] to add/update content for a specific quote id.
Adding a new quote, returns the id of the new quote record. A new quote record can be created by adding data to any column of a new record. This returns a new quote record id, for example id 37.
quote new author "Megan Davis"Add/update the data to other columns of the new quote using
quote update [col] [value] [id]Eg. to add data to the reference ref and date date columns for new quote id 37:
quote update ref "https://www.themonthly.com.au/september-2025/nation-reviewed/garma-chameleon" 37
quote update date "01/09/2025" 37
quote update quote "This is my quote text" 37Update data in a quote record by using quote update... to change data in specific columns:
quote update [column] [value] [id]A random quote can be obtained with:
get_quoteIt can be printed including date and reference url with:
get_quote -d -rI learned from the plants I cared for and the forests I walked. But my greatest teacher was the sea kayak. To be on the water changes your experience... It's ordinary... for your days to be defined by the rhythms of the tides and the whims of the weather, ordinary not to produce or consume but simply to be present. The act of paddling... felt like a return to. the world I loved as a kid. It felt like coming to life again.
--Lauren Fuge (https://www.textpublishing.com.au/books/voyagers-our-journey-into-the-anthropocene, 2024)Search parameters can be provided to filter the data. The search parameter is matched on a case insensitive contains basis.
To return a random quote which contains the word "biodiversity":
get_quote -q "biodiversity"To return a random quote by Megan Davis use:
get_quote -a "Megan Davis"A database dump in sql.gz format is written to ~/quotes-[TODAY].sql.gz with:
bash ./db/backup_db.shAdd to crontab to create a regular backup.
Restore the backup to this test database:
bash db/restore_db.sh ~/quotes-[LAST_DATE].sql.gzThe end of the output shows the number of rows in the table:
setval
--------
37Test the database by opening an interactive terminal with psql and check that there are the correct data and number of records in the backup:
docker compose exec -it [POSTGRES_SERVICE_NAME] psql -U [POSTGRES_USER] [POSTGRES_DB]
select id,author from quotes;
select count(*) from quotes;This can be used to create a quote db table from scratch using a json file quotes.json. See template file provided.
Template: templates/quotes_template.json
First minify the json file such that the json array is on a single line. This is required for the postgres function json_populate_recordset() to successfully read the json array.
Upload the ~/quotes.json in https://jsonformatter.org/ and click Minify / Compact.
Click Download to download the minified json file to ~/Downloads/jsonformatter.txt.
The quotes table can be created and the json file read into the table using ./initdb folder. Data and scripts in the folder are copied across to the container and run when the container is created. Files run in alpha numerical filename order.
Uncomment this folder in the volumes section of docker-compose.yml.
Copy the minified quotes file to ./initdb/quotes.json:
cp ~/Downloads/jsonformatter.txt initdb/quotes.jsonOn recreating the container the sql initdb/import_quotes_json.sql will create the quotes table and import json data into the quote table:
docker compose up --detachVerify that table now contains the number of rows in the json file with:
docker compose exec -it [POSTGRES_SERVICE_NAME] psql -U [POSTGRES_USER] -d [POSTGRES_DB]
\dt
\d quotes
select count(*) from quotes;
\q