Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

2. Persist notes in PostgreSQL

Replace hello with the notes checkpoint, attach PostgreSQL, and create the schema once. The Redis and S3 code is present but is not used until you request an export.

1. Replace the source checkpoint

Download the complete notes ZIP beside your hello directory. From inside nodejs-hello, run:

cd ..
unzip nodejs-notes.zip
cp -R nodejs-notes/. nodejs-hello/
cd nodejs-hello
rm server.mjs
npm ci

This retains the hello directory’s .git history and shared remote. The rm command removes only the superseded hello source file in this tutorial directory. The source appendix shows the database implementation. Notes use UUID IDs, validated text, and parameterized SQL or the MongoDB driver; no file on the app instance stores notes. For an optional local run, follow Local services and run node app.mjs migrate before npm start.

2. Create and attach PostgreSQL

Dashboard: in nodejs add a Database named notes-db, engine PostgreSQL, with 1 GB storage. Wait until running. Open notes-web → Config → Attached Resources, select notes-db, leave the variable name blank (DATABASE_URL), and click Attach. In the same Config tab add plain variable DATABASE_ENGINE=postgresql.

CLI:

paas dbs create notes-db --type postgresql --storage-gb 1 -o "$TUTORIAL_ORG" -p examples -e nodejs
paas dbs info notes-db -o "$TUTORIAL_ORG" -p examples -e nodejs
paas apps attach notes-web notes-db -o "$TUTORIAL_ORG" -p examples -e nodejs
paas config set DATABASE_ENGINE=postgresql -a notes-web -o "$TUTORIAL_ORG" -p examples -e nodejs

Wait for database provisioning before trying to initialize the schema. The attachment injects DATABASE_URL; never copy credentials into the source or .env in Git. Check attachment names with paas apps attachments notes-web -o "$TUTORIAL_ORG" -p examples -e nodejs.

3. Deploy the notes app

git add .
git commit -m "Add persistent notes and export processes"
git push d2o main

Dashboard: wait for notes-web to be running in its Deployments/Overview view. CLI: run paas watch -a notes-web -o "$TUTORIAL_ORG" -p examples -e nodejs. /health succeeds even before the schema exists; /notes may return 503 until the next step.

4. Initialize the schema

Dashboard: notes-web → Console → select a running instance → Connect. Run:

/cnb/process/migrate

CLI:

paas run -a notes-web -o "$TUTORIAL_ORG" -p examples -e nodejs -- /cnb/process/migrate

The migrate process is declared in the Procfile. Calling /cnb/process/migrate loads the buildpack runtime environment, including the language executable and dependencies. Expect Schema ready. The command creates the notes table if missing, so it is safe to repeat for this example. paas run does not relay the remote exit code: inspect the output and verify /notes, rather than assuming a zero CLI exit means the schema succeeded.

5. Check persistence

Open the app URL, enter a sample note, and choose Save note. Refresh; the note remains. You can also test from your terminal:

curl -fsS -H "Content-Type: application/json" -d '{"body":"My first persistent note"}' "$APP_URL/notes"
curl -fsS "$APP_URL/notes"

Restart notes-web from its dashboard action or run paas restart -a notes-web -o "$TUTORIAL_ORG" -p examples -e nodejs. Wait for it to be running and fetch /notes again; the note must still be present. An empty body returns HTTP 400. Leave Export notes for the next chapter.

For production, replace manual schema setup with versioned release-command migrations.

Next: Add a Redis export worker.