Welcome back, young data guardian! In Module 1, you learned what a database is. In Module 2, you learned how to install PostgreSQL and create databases. In Module 3, you learned about joins, subqueries, views, and normalization. In Module 4, you learned about performance tuning and optimization.
In this module, we are going to learn about security, backup, and replication. Security means keeping your data safe from bad people. Backup means making copies of your data. Replication means copying your data to another computer so it is always available. These are the most important jobs of a database expert.
Do not worry if some words sound big. We will explain everything in simple language. We will use stories, examples from Nigeria, examples from school, and examples from home. We will use pictures made from keyboard characters (ASCII art) to help you see what we mean. By the end of this module, you will know:
This module is long because it is a complete chapter, not just short notes. Take your time. Read one lesson at a time. Do the exercises. Ask questions. Practice on your computer. Are you ready? Let us begin!
After finishing this module, you should be able to:
Fatima was a 13-year-old girl who lived in Abuja, Nigeria. Her aunt worked at a big bank. One day, Fatima's aunt came home looking very worried. "The bank's database was attacked," she said. "Someone tried to steal customer information."
Fatima was shocked. "Did they succeed?" she asked.
Her aunt shook her head. "No. We had good security. We use strong passwords. We use SSL. We use row-level security. The attacker could not get in."
Fatima was relieved. "What about the data?" she asked. "Is it safe?"
Her aunt smiled. "Yes. We back up the database every night. We also have a copy on another server. Even if the main server fails, we can switch to the backup. This is called replication."
Fatima was amazed. She decided to learn everything she could about database security, backup, and replication. She wanted to protect data like her aunt.
Moral of the story: Security, backup, and replication keep data safe. Anyone can learn to protect a database.
Database security means keeping a database safe from bad people. It means stopping people from stealing, changing, or destroying data.
Data is valuable. It can be sold. It can be used to hurt people. A bank's data must be safe. A school's data must be safe. Your family's data must be safe.
Imagine you have a treasure chest. You lock it with a key. You hide the key. Database security is like locking the treasure chest.
A bank uses strong security to protect customer accounts.
A school uses security to protect student scores.
At home, you use a password to protect your recipe database.
In a Nigerian bank, security experts protect customer data from thieves.
DATABASE SECURITY:
[ DATABASE ]
|
V
[ STRONG PASSWORDS ]
|
V
[ SSL ENCRYPTION ]
|
V
[ ROW-LEVEL SECURITY ]
|
V
[ DATA IS SAFE ]
Database security keeps data safe from bad people. It stops people from stealing, changing, or destroying data.
You learned what database security is. You saw examples from home, school, and Nigeria. Now you know that security keeps data safe.
A user is a person who can connect to a database. A role is a set of permissions. You give roles to users.
Not everyone should have full access to a database. Some people should only read data. Some people should only write data. Users and roles help you control who can do what.
Imagine you have a house. You give different keys to different people. Your family gets a key to all rooms. Visitors get a key only to the living room. Users and roles are like different keys.
A bank gives tellers a role that can read customer accounts. The bank gives managers a role that can change accounts.
A school gives teachers a role that can add scores. The school gives students a role that can only view their own scores.
At home, you give parents a role that can change recipes. You give children a role that can only view recipes.
In a Nigerian bank, tellers have a role that can read accounts. Managers have a role that can change accounts.
-- Create a role CREATE ROLE teller WITH LOGIN PASSWORD 'password123'; -- Grant permissions GRANT SELECT ON customers TO teller; GRANT INSERT, UPDATE ON sales TO teller;
USERS AND ROLES:
[ ADMIN ] --> [ FULL ACCESS ]
|
V
[ MANAGER ] --> [ READ AND WRITE ]
|
V
[ TELLER ] --> [ READ ONLY ]
|
V
[ STUDENT ] --> [ VIEW ONLY ]
Users are people who connect to a database. Roles are sets of permissions. They help you control who can do what.
You learned how to create users and roles. You saw examples from home, school, and Nigeria. Now you can control access to your database.
Granting means giving permission. Revoking means taking permission away. You use GRANT and REVOKE to control access.
You must give people the right permissions. Too much permission is dangerous. Too little permission stops work.
Imagine you have a box of chocolates. You give your friend one chocolate. That is granting. Later, you take it back. That is revoking.
A bank grants tellers permission to read accounts. It revokes permission when they leave.
A school grants teachers permission to add scores. It revokes permission when they retire.
At home, you grant children permission to view recipes. You revoke permission if they misuse it.
In a Nigerian bank, managers grant permission to new staff. They revoke permission when staff leave.
-- Grant permission GRANT SELECT ON customers TO teller; -- Revoke permission REVOKE SELECT ON customers FROM teller;
GRANT AND REVOKE:
[ USER ] --> [ GRANT: GIVE PERMISSION ]
|
V
[ USER CAN READ DATA ]
|
V
[ REVOKE: TAKE PERMISSION ]
|
V
[ USER CANNOT READ DATA ]
Granting gives permission. Revoking takes permission away. You use GRANT and REVOKE to control access.
You learned how to grant and revoke permissions. You saw examples from home, school, and Nigeria. Now you can control who can do what in your database.
SSL stands for Secure Sockets Layer. It is a way to encrypt data that travels between a user and a database. Encryption means the data is scrambled so no one can read it.
Without SSL, data travels in plain text. Anyone can read it. With SSL, data travels in secret code. Only the right people can read it.
Imagine you are sending a letter. Without SSL, anyone can open the letter and read it. With SSL, the letter is locked in a box. Only the receiver has the key.
A bank uses SSL to protect customer data when they log in online.
A school uses SSL to protect student data when teachers access it.
At home, you use SSL when you shop online.
In a Nigerian bank, SSL protects customer data when they use online banking.
-- Check if SSL is enabled SHOW ssl; -- Connect with SSL psql "sslmode=require host=myserver dbname=mydb user=myuser"
WITHOUT SSL:
[ USER ] --> [ PLAIN TEXT ] --> [ DATABASE ]
| |
V V
[ ANYONE CAN READ ]
WITH SSL:
[ USER ] --> [ ENCRYPTED DATA ] --> [ DATABASE ]
| |
V V
[ ONLY RIGHT PEOPLE CAN READ ]
SSL encrypts data that travels between a user and a database. It stops people from reading your data.
You learned about SSL. You saw examples from home, school, and Nigeria. Now you know how to protect data in transit.
Row-level security (RLS) is a way to control which rows a user can see. You write rules that say: "This user can only see rows where the customer_id matches their own."
Sometimes, users should only see their own data. For example, a customer should only see their own orders. They should not see other customers' orders.
Imagine you have a stack of letters. Each letter is for a different person. You only give each person their own letter. Row-level security is like that.
A bank uses row-level security. A customer can only see their own account.
A school uses row-level security. A student can only see their own scores.
At home, you use row-level security. Each child can only see their own chores.
In a Nigerian bank, row-level security stops customers from seeing other customers' accounts.
-- Enable row-level security ALTER TABLE customers ENABLE ROW LEVEL SECURITY; -- Create a policy CREATE POLICY customer_policy ON customers FOR SELECT USING (customer_id = current_user_id()); -- Grant permission GRANT SELECT ON customers TO customer_role;
ROW-LEVEL SECURITY:
[ CUSTOMER 1 ] --> [ SEES ONLY ROW 1 ]
[ CUSTOMER 2 ] --> [ SEES ONLY ROW 2 ]
[ CUSTOMER 3 ] --> [ SEES ONLY ROW 3 ]
|
V
[ EACH CUSTOMER SEES ONLY THEIR OWN DATA ]
Row-level security controls which rows a user can see. It stops users from seeing other users' data.
You learned about row-level security. You saw examples from home, school, and Nigeria. Now you can protect data at the row level.
A backup is a copy of your database. You make a backup so you can restore your data if something goes wrong.
Disasters happen. A hard drive can fail. A fire can burn a building. A hacker can delete data. A backup helps you recover.
Imagine you have a favourite toy. You buy a second one. If you lose the first, you still have the second. A backup is like that second toy.
A bank backs up its database every night. If the main database fails, the bank restores the backup.
A school backs up its database every week. If the computer fails, the school restores the backup.
At home, you back up your recipes to a USB drive. If your computer fails, you restore the recipes.
In a Nigerian bank, backups are stored in a safe place. If the main database fails, the bank restores the backup.
BACKUP:
[ DATABASE ]
|
V
[ CREATE BACKUP ]
|
V
[ STORE IN SAFE PLACE ]
|
V
[ IF DISASTER HAPPENS ]
|
V
[ RESTORE FROM BACKUP ]
A backup is a copy of your database. It helps you recover if something goes wrong.
You learned what a backup is. You saw examples from home, school, and Nigeria. Now you know why backups are important.
pg_dump is a tool that creates a backup of a PostgreSQL database. It saves the database to a file.
pg_dump is easy to use. It works for small and medium databases. You can use it to move data from one computer to another.
Imagine you want to copy a book. You use a photocopier. pg_dump is like a photocopier for your database.
A bank uses pg_dump to back up customer accounts every night.
A school uses pg_dump to back up student scores every week.
At home, you use pg_dump to back up your recipes every month.
In a Nigerian bank, pg_dump is used to back up customer data.
-- Backup a database pg_dump -U postgres -d myshop -f myshop_backup.sql -- Backup all databases pg_dumpall -U postgres -f all_databases_backup.sql
pg_dump:
[ DATABASE ]
|
V
[ RUN pg_dump ]
|
V
[ BACKUP FILE CREATED ]
|
V
[ STORE IN SAFE PLACE ]
pg_dump creates a backup of a PostgreSQL database. It saves the database to a file.
You learned how to use pg_dump. You saw examples from home, school, and Nigeria. Now you can back up your database.
Restoring means loading a backup into a database. You restore when you lose data or when you move to a new computer.
A backup is useless if you cannot restore it. Restoring brings your data back.
Imagine you have a copy of a book. You lose the original. You use the copy to make a new original. Restoring is like making a new original from the copy.
A bank restores its database after a hard drive failure.
A school restores its database after a computer crash.
At home, you restore your recipes after your computer fails.
In a Nigerian bank, the database is restored after a disaster.
-- Restore a database psql -U postgres -d myshop -f myshop_backup.sql -- Create a new database and restore createdb -U postgres myshop_new psql -U postgres -d myshop_new -f myshop_backup.sql
RESTORE:
[ BACKUP FILE ]
|
V
[ RUN RESTORE COMMAND ]
|
V
[ DATABASE IS RECREATED ]
|
V
[ DATA IS BACK! ]
Restoring means loading a backup into a database. It brings your data back.
You learned how to restore a database. You saw examples from home, school, and Nigeria. Now you can restore your database from a backup.
Replication means copying data from one database to another. The first database is called the primary. The second database is called the replica.
Replication keeps your data safe. If the primary fails, the replica can take over. Replication also helps with performance. Many users can read from the replica.
Imagine you have a notebook. You make a copy in another room. If you lose the first notebook, you still have the copy. Replication is like that.
A bank replicates its database to another city. If the main database fails, the bank switches to the replica.
A school replicates its database to another building. If the main database fails, the school switches to the replica.
At home, you replicate your recipes to another computer. If one fails, you use the other.
In a Nigerian bank, the database is replicated to another server. If the main server fails, the bank switches to the replica.
REPLICATION:
[ PRIMARY DATABASE ]
|
V
[ COPY DATA ]
|
V
[ REPLICA DATABASE ]
|
V
[ IF PRIMARY FAILS ]
|
V
[ SWITCH TO REPLICA ]
Replication copies data from one database to another. It keeps your data safe and available.
You learned what replication is. You saw examples from home, school, and Nigeria. Now you know how replication keeps data safe.
Streaming replication is a type of replication. The primary sends data to the replica continuously. The replica stays up to date.
Streaming replication keeps the replica almost identical to the primary. If the primary fails, the replica can take over quickly.
Imagine you are watching a live football match. The match is happening now. Streaming replication is like watching the match live. You see everything as it happens.
A bank uses streaming replication. The replica is always up to date.
A school uses streaming replication. The replica is always up to date.
At home, you use streaming replication. The replica is always up to date.
In a Nigerian bank, streaming replication keeps the replica up to date.
-- On the primary ALTER SYSTEM SET wal_level = replica; ALTER SYSTEM SET max_wal_senders = 3; -- On the replica pg_basebackup -h primary_host -D /var/lib/postgresql/data -U replicator -P -v
STREAMING REPLICATION:
[ PRIMARY ] -- WAL DATA --> [ REPLICA ]
| |
V V
[ WRITE DATA ] [ REPLICA UPDATED ]
| |
V V
[ REPLICA IS ALWAYS UP TO DATE ]
Streaming replication sends data continuously from the primary to the replica. The replica stays up to date.
You learned about streaming replication. You saw examples from home, school, and Nigeria. Now you can set up streaming replication.
Point-in-time recovery (PITR) means restoring a database to a specific moment in time. You can restore to before a mistake happened.
Mistakes happen. Someone might delete important data. PITR lets you go back to before the mistake.
Imagine you are watching a movie. You want to go back to a scene you missed. You rewind the movie. PITR is like rewinding your database.
A bank uses PITR. A teller makes a mistake. The bank rewinds to before the mistake.
A school uses PITR. A teacher deletes scores by mistake. The school rewinds to before the mistake.
At home, you use PITR. You delete a recipe by mistake. You rewind to before the mistake.
In a Nigerian bank, PITR is used to recover from mistakes.
-- Set recovery target time restore_command = 'cp /backup/wal/%f %p' recovery_target_time = '2025-01-15 10:00:00'
POINT-IN-TIME RECOVERY:
[ DATABASE AT 10:00 AM ]
|
V
[ MISTAKE HAPPENS AT 10:05 AM ]
|
V
[ RESTORE TO 10:00 AM ]
|
V
[ MISTAKE IS UNDONE ]
Point-in-time recovery restores a database to a specific moment. It undoes mistakes.
You learned about point-in-time recovery. You saw examples from home, school, and Nigeria. Now you can recover from mistakes.
High availability means keeping a database running almost all the time. It means having backups and replicas ready.
If a database stops, users cannot work. High availability keeps the database running.
Imagine you have a torch. You keep spare batteries. If the batteries die, you replace them. High availability is like keeping spare batteries for your database.
A bank uses high availability. If the main server fails, the replica takes over.
A school uses high availability. If the main computer fails, the replica takes over.
At home, you use high availability. If one computer fails, you use another.
In a Nigerian bank, high availability keeps the database running even if a server fails.
HIGH AVAILABILITY:
[ PRIMARY SERVER ]
|
V
[ REPLICA SERVER ]
|
V
[ IF PRIMARY FAILS ]
|
V
[ REPLICA TAKES OVER ]
|
V
[ DATABASE KEEPS RUNNING ]
High availability keeps a database running almost all the time. It uses backups and replicas.
You learned about high availability. You saw examples from home, school, and Nigeria. Now you know how to keep your database running.
Best practices are the best ways to do things. They help you keep your database safe.
Following best practices prevents problems. It keeps your data safe from thieves.
Imagine you are locking your house. You use a strong lock. You close all windows. Best practices for security are like locking your house properly.
A bank follows best practices. Its data is safe.
A school follows best practices. Its data is safe.
At home, you follow best practices. Your data is safe.
In a Nigerian bank, best practices keep customer data safe.
BEST PRACTICES FOR SECURITY:
[ STRONG PASSWORDS ] --> [ SSL ]
| |
V V
[ ROW-LEVEL SECURITY ] --> [ LIMITED PERMISSIONS ]
| |
V V
[ REGULAR BACKUPS ] --> [ SAFE STORAGE ]
| |
V V
[ REPLICATION ] --> [ MONITORING ]
| |
V V
[ SAFE DATABASE! ]
Best practices help you keep your database safe. Follow them to prevent problems.
You learned best practices for security. You saw a list. Now you can keep your database safe.
Best practices for backup and replication are the best ways to back up and copy your data.
Good backups and replication keep your data safe. They help you recover from disasters.
Imagine you have a spare key to your house. You keep it in a safe place. Best practices for backup and replication are like keeping spare keys.
A bank follows best practices. It can recover from any disaster.
A school follows best practices. It can recover from any disaster.
At home, you follow best practices. You can recover from any disaster.
In a Nigerian bank, best practices for backup and replication keep data safe.
BEST PRACTICES FOR BACKUP AND REPLICATION:
[ REGULAR BACKUPS ] --> [ MULTIPLE LOCATIONS ]
| |
V V
[ TEST BACKUPS ] --> [ pg_dump / pg_basebackup ]
| |
V V
[ STREAMING REPLICATION ] --> [ PITR ]
| |
V V
[ MONITOR LAG ] --> [ DOCUMENT PLAN ]
| |
V V
[ SAFE AND AVAILABLE DATA! ]
Best practices for backup and replication keep your data safe. Follow them to recover from disasters.
You learned best practices for backup and replication. You saw a list. Now you can keep your data safe and available.
Putting it all together means using all your security, backup, and replication skills. It means making your database safe, recoverable, and always available.
A database that is safe, recoverable, and available makes users happy. It helps businesses work better.
Imagine you are building a house. You need strong walls, a good roof, and a spare key. Putting it all together means building a safe house. In the same way, you build a safe database with security, backup, and replication.
A bank puts it all together. Its database is safe, recoverable, and always available.
A school puts it all together. Its database is safe, recoverable, and always available.
At home, you put it all together. Your database is safe, recoverable, and always available.
In a Nigerian bank, the database is safe, recoverable, and always available.
PUTTING IT ALL TOGETHER:
[ USERS AND ROLES ]
|
V
[ GRANT AND REVOKE ]
|
V
[ SSL ENCRYPTION ]
|
V
[ ROW-LEVEL SECURITY ]
|
V
[ REGULAR BACKUPS ]
|
V
[ STREAMING REPLICATION ]
|
V
[ HIGH AVAILABILITY ]
|
V
[ SAFE, RECOVERABLE, ALWAYS AVAILABLE DATABASE! ๐ ]
Putting it all together means using all your skills. It makes your database safe, recoverable, and always available.
You learned how to put everything together. You saw examples from home, school, and Nigeria. Now you can protect your database like a professional.
| Word | Simple Definition |
|---|---|
| Database security | Keeping a database safe from bad people. |
| User | A person who can connect to a database. |
| Role | A set of permissions given to users. |
| Grant | Giving permission. |
| Revoke | Taking permission away. |
| SSL | Secure Sockets Layer. A way to encrypt data in transit. |
| Row-level security | Controlling which rows a user can see. |
| Backup | A copy of your database. |
| pg_dump | A tool that creates a backup of a PostgreSQL database. |
| Restore | Loading a backup into a database. |
| Replication | Copying data from one database to another. |
| Streaming replication | Continuous copying of data from primary to replica. |
| Point-in-time recovery | Restoring a database to a specific moment. |
| High availability | Keeping a database running almost all the time. |
DATABASE
|
V
[ USERS AND ROLES ]
|
V
[ GRANT AND REVOKE ]
|
V
[ SSL ENCRYPTION ]
|
V
[ ROW-LEVEL SECURITY ]
|
V
[ REGULAR BACKUPS ]
|
V
[ REPLICATION ]
|
V
[ HIGH AVAILABILITY ]
|
V
[ SAFE DATABASE! ]
Step 1: Learn what database security is. Step 2: Learn to create users and roles. Step 3: Learn to grant and revoke permissions. Step 4: Learn about SSL. Step 5: Learn about row-level security. Step 6: Learn what a backup is. Step 7: Learn to use pg_dump. Step 8: Learn to restore a database. Step 9: Learn what replication is. Step 10: Learn about streaming replication. Step 11: Learn about point-in-time recovery. Step 12: Learn about high availability. Step 13: Learn best practices for security. Step 14: Learn best practices for backup and replication. Step 15: Put it all together.
[ PRIMARY DATABASE ]
|
V
[ BACKUP ] --> [ STORE IN SAFE PLACE ]
|
V
[ REPLICA DATABASE ]
|
V
[ IF PRIMARY FAILS ]
|
V
[ SWITCH TO REPLICA ]
|
V
[ DATABASE KEEPS RUNNING ]
| Feature | What It Does |
|---|---|
| Users and roles | Control who can connect |
| Grant and revoke | Control what users can do |
| SSL | Encrypt data in transit |
| Row-level security | Control which rows users can see |
| Backups | Copy data for recovery |
| Replication | Copy data to another server |
| Point-in-time recovery | Restore to a specific moment |
| High availability | Keep database running |
| Feature | Without Security | With Security |
|---|---|---|
| Passwords | Weak | Strong |
| SSL | No | Yes |
| Row-level security | No | Yes |
| Backups | Rare | Regular |
| Replication | No | Yes |
| High availability | No | Yes |
| Data safety | Low | High |
In this module, you learned about security, backup, and replication. You learned that database security keeps data safe from bad people. You learned how to create users and roles. You learned how to grant and revoke permissions. You learned about SSL. It encrypts data in transit. You learned about row-level security. It controls which rows a user can see.
You learned about backups. A backup is a copy of your database. You learned how to use pg_dump to create a backup. You learned how to restore a database. You learned about replication. Replication copies data from one database to another. You learned about streaming replication. It keeps the replica up to date. You learned about point-in-time recovery. It restores a database to a specific moment. You learned about high availability. It keeps a database running almost all the time.
You learned best practices for security, backup, and replication. You learned how to put it all together to make a safe, recoverable, and always available database.
Now you are ready to move on to Module 6, where you will learn about advanced PostgreSQL features. But before you go, make sure you understand the key takeaways below.
Match the word in Column A with the correct definition in Column B.
| Column A | Column B |
|---|---|
| 1. Database security | A. A copy of your database |
| 2. Role | B. Copying data from one database to another |
| 3. Grant | C. Keeping a database safe from bad people |
| 4. SSL | D. A set of permissions given to users |
| 5. Row-level security | E. Giving permission |
| 6. Backup | F. Encrypting data in transit |
| 7. Replication | G. Controlling which rows a user can see |
| 8. High availability | H. Keeping a database running almost all the time |
Answers: 1-C, 2-D, 3-E, 4-F, 5-G, 6-A, 7-B, 8-H
Title: Design a Security Plan
Instructions: In groups of 3 or 4, design a security plan for a school database.
List the users and roles. List the permissions. List the backup plan. List the replication plan.
Present your plan to the class.
Example:
Users: admin, teacher, student
Roles: admin (full access), teacher (read and write scores), student (view own scores)
Backup: daily pg_dump to external drive
Replication: streaming replication to a second server
Title: My Backup Plan
Instructions: Write a short plan for backing up your database.
List at least 5 steps. Explain why each step is important.
Example:
1. Back up the database every night using pg_dump.
2. Store the backup on an external hard drive.
3. Store a second copy in the cloud.
4. Test the backup every week.
5. Document the restore process.
Title: Backup and Restore a Database
Materials: Computer with PostgreSQL installed.
Steps:
Assignment: For one week, practice security and backup tasks. Keep a diary. Each day, write down:
At the end of the week, write a short report. Share it with your teacher or parent.
In Module 6, you will learn about advanced PostgreSQL features. You will learn about:
Before you start Module 6, make sure you understand everything in this module. Practice creating users and roles. Practice granting and revoking permissions. Back up and restore a database. Set up replication. Monitor your database. When you are ready, turn the page and continue your journey to becoming a Certified PostgreSQL Expert!
End of Module 5.