Level 5 · Databases and Data Management · Course 5.2 SQL

Module 58: Database Administration

Goal: look after a database properly: control who can do what with users and roles, change the structure safely with migrations, and make backups you've actually proved you can restore.

You'll need: your crm.db and the SQL from Modules 55 to 57. (Users and roles need PostgreSQL; you'll practise them in Module 60.)

Lessons

  1. 1Users, roles and permissions15–25 minutes✓
  2. 2Migrations15–25 minutes✓
  3. 3Backups15–25 minutes✓
  4. 4Restoring15–25 minutes✓
  5. 5Everyday care15–25 minutes✓

🛠️Module practice: Project B, an administered CRM database

  1. Roles and permissions planned in admin.sql (run in Module 60).
  2. The whole schema as numbered migration files; database rebuilt from them.
  3. Automated backup command and a 3-2-1 plan with RPO and RTO.
  4. A tested, timed restore with a written guide.
  5. An admin.md runbook. Commit.

✅Module check

Explain in a few sentences, in your own words:

What does it take to look after a database responsibly?

Show a good answer

A good answer mentions: least-privilege roles (the app, migrations and reporting each with only the permissions they need, no everyday superuser); structure changes only through ordered, version-controlled migrations that are never edited after running and are made in safe steps on live systems; automated, encrypted backups following 3-2-1 with clear RPO and RTO; regular test restores into a separate database, timed and documented; plus monitoring, maintenance, careful handling of personal data, and written runbooks.

📝Module quiz

10 questions · 10 marks each · 100 marks in all · pass mark 70% · retry as often as you like ·

1. Should your app connect to the database as the superuser?

Why each option is right or wrong

  • “Yes, so the app never hits a permission error” (Incorrect): Avoiding errors this way removes an important safety net.
  • “Yes, as long as the password is long and random” (Incorrect): A strong password doesn't limit what a compromised app can do.
  • “No, apps should connect without any password at all” (Incorrect): Apps need credentials; they should belong to a limited role.
  • “No, use a role with only the permissions it needs” is correct.

    A superuser can drop tables, read every database and change settings. If the app is compromised, or a bug runs the wrong query, the damage is unlimited. A dedicated role that can only read and write the app's tables limits what can go wrong.

    • Separate roles: app, migrations, read-only reporting.
    • Least privilege: each gets only what its job needs.

📖 Revise: Lesson 1: Users, roles and permissions

Question ID module-58-q1 · Report a mistake in this question
2. Why give reporting tools read-only access?

Why each option is right or wrong

  • “Read-only access makes reports load faster” (Incorrect): Speed isn't the reason; safety is.
  • “Reporting tools can't write data even with full access” (Incorrect): Many can run any SQL they're allowed to.
  • “It hides the data from the report's readers” (Incorrect): They can still read; they just can't change anything.
  • “A mistake can't then change or delete data” is correct.

    Reports are often written quickly by different people. With read-only access, a wrong query can only read too much or too little; it can't update or delete anything.

    • Even safer: point reports at a read replica.
    • Also limit: which tables and columns they can read.

📖 Revise: Lesson 1: Users, roles and permissions

Question ID module-58-q2 · Report a mistake in this question
3. A migration already ran on the test server and has a typo. What do you do?

Why each option is right or wrong

  • “Edit the original migration file and run it again” (Incorrect): Databases that already ran it won't rerun it, so copies drift apart.
  • “Delete the migration file and fix the database by hand” (Incorrect): Manual fixes aren't recorded and won't reach other copies.
  • “Drop the test database and ignore the typo in production” (Incorrect): The typo would still be in production's structure.
  • “Write a new migration that fixes it” is correct.

    Once a migration has run anywhere, changing the file means that database and others no longer agree with the history. A new migration that corrects the mistake runs everywhere in the same order.

    • Unrun migrations: can still be edited before anyone applies them.
    • History: stays a true record of every change.

📖 Revise: Lesson 2: Migrations

Question ID module-58-q3 · Report a mistake in this question
4. How do you add a required column to a big, live table safely?

Why each option is right or wrong

  • “Add it as NOT NULL in one step and accept the error” (Incorrect): Existing rows have no value, so the change fails or locks the table.
  • “Copy the whole table, add the column, then swap them over” (Incorrect): Possible, but heavier and riskier than staged changes.
  • “Add it only in the app's code, not in the database” (Incorrect): The database then can't enforce the rule.
  • “Add it nullable, fill values, then add NOT NULL” is correct.

    Adding a NOT NULL column directly fails if existing rows have no value, and filling millions of rows in one step can lock the table. Doing it in stages, nullable first, then filling in batches, then adding the constraint, keeps the app running.

    • Called: expand and contract.
    • Deploy code: that writes the new column before making it required.

📖 Revise: Lesson 2: Migrations

Question ID module-58-q4 · Report a mistake in this question
5. Backups are kept on the same server as the database. What's the problem?

Why each option is right or wrong

  • “Backups on the same server make the database slower” (Incorrect): Speed isn't the main risk; losing both together is.
  • “Backups can't be read on the server that made them” (Incorrect): They can; the issue is losing both at once.
  • “Nothing; it's the most convenient and safe place for them” (Incorrect): Convenient, but one failure takes out both.
  • “If the server fails or is hacked, backups go with it” is correct.

    A backup on the same machine protects against a bad query, but not against disk failure, fire, ransomware or a deleted server. Keep copies somewhere separate, ideally in another location and account.

    • 3-2-1 rule: 3 copies, 2 types of storage, 1 off-site.
    • Protect them: limit who can delete backups.

📖 Revise: Lesson 3: Backups

Question ID module-58-q5 · Report a mistake in this question
6. What's an RPO?

Why each option is right or wrong

  • “How long it takes to restore the database” (Incorrect): That's the recovery time objective (RTO).
  • “The number of backup copies you keep” (Incorrect): That's retention, not RPO.
  • “The password used to encrypt the backups” (Incorrect): RPO is a time target, not a password.
  • “The most recent data you can afford to lose” is correct.

    Recovery point objective: if backups run every 24 hours, you could lose up to a day of data. If that's too much, you need more frequent backups or continuous archiving.

    • RTO: recovery time objective, how long recovery may take.
    • Set them with the business: they drive backup choices.

📖 Revise: Lesson 4: Restoring

Question ID module-58-q6 · Report a mistake in this question
7. Why should backups be tested by restoring them?

Why each option is right or wrong

  • “Backups expire unless they're restored once a month” (Incorrect): Backups don't expire; untested ones may simply not work.
  • “Restoring makes the backup smaller” (Incorrect): Restoring doesn't change the backup.
  • “Hosting providers require it before backups are kept” (Incorrect): It's your safeguard, not a hosting rule.
  • “They can fail silently; only a restore proves they work” is correct.

    Backup jobs can produce empty files, miss a database, or use a format the restore tool rejects, all without obvious errors. Restoring to a test server regularly shows the backup is complete and that you know the steps.

    • Schedule it: for example monthly.
    • Time it: to check your RTO is realistic.

📖 Revise: Lesson 4: Restoring

Question ID module-58-q7 · Report a mistake in this question
8. Someone deleted 30 customers this morning. Should you restore the whole database over production?

Why each option is right or wrong

  • “Yes, a full restore is the quickest way to fix it” (Incorrect): Quick, but it throws away everything since the backup.
  • “Yes, as long as the backup is from last night” (Incorrect): Even a recent backup loses this morning's other changes.
  • “No, deleted rows can't be recovered from backups” (Incorrect): They can, from a backup restored elsewhere.
  • “No, restore elsewhere and copy back only the lost rows” is correct.

    Restoring over production would undo every change since the backup, losing this morning's new orders and updates. Restoring to a separate database and copying back just the 30 customers fixes the mistake without new losses.

    • Check related rows: their orders or notes may need restoring too.
    • Then: find out how the delete happened.

📖 Revise: Lesson 4: Restoring

Question ID module-58-q8 · Report a mistake in this question
9. What happens to most databases when the disk fills up?

Why each option is right or wrong

  • “They delete the oldest rows to make space” (Incorrect): Databases don't delete your data to free space.
  • “They compress all the data automatically” (Incorrect): They don't; writes fail instead.
  • “Nothing; databases don't need disk space after setup” (Incorrect): They need space for every write and for their logs.
  • “They stop accepting writes, or stop entirely” is correct.

    Databases need disk space for data, logs and temporary work. When it runs out, writes fail and the database may shut down to protect itself, taking the app with it. Monitoring disk space with an alert well before it fills avoids this.

    • Alert at: for example 80% full.
    • Watch for: logs and old backups filling the disk.

📖 Revise: Lesson 5: Everyday care

Question ID module-58-q9 · Report a mistake in this question
10. A managed database does backups for you. What's still your job?

Why each option is right or wrong

  • “Nothing; the provider handles everything” (Incorrect): Providers handle infrastructure; your data and access remain your responsibility.
  • “Running your own backups every hour as well” (Incorrect): Extra backups can help, but testing restores and access matter more.
  • “Replacing the server's disks when they fail” (Incorrect): Managed services handle the hardware.
  • “Testing restores, access control and knowing recovery steps” is correct.

    The provider runs backups, but only you know whether restores contain what you need, who should have access, and how to recover quickly under pressure. Practise the steps before you need them.

    • Also yours: schema design, indexes and query performance.
    • Write a runbook: the recovery steps, in order.

📖 Revise: Lesson 5: Everyday care

Question ID module-58-q10 · Report a mistake in this question

ProLearners Assist