PgAdmin: Working with Tables, Columns, Constraints, Indexes, RLS Policy, Rules, and Triggers
Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).
PgAdmin tutorial to work with Tables, Columns, Constraints, Indexes, RLS Policy, Rules, and Triggers by creating and managing an Employees table.
1. Creating the Employees Table
Before working with columns, constraints, and indexes, we need to create the Employees table.
Steps in PgAdmin:
Open PgAdmin and connect to your database.
In the Object Browser, right-click Tables under your schema (e.g.,
public).Click Create → Table.
In the General tab, enter:
Name:
EmployeesSchema:
public
Click Save.
2. Adding Columns to Employees Table
Steps in PgAdmin:
In the Object Browser, expand Tables → Employees.
Right-click Columns and select New Column.
Fill in the column details:
Name:
employee_idData Type:
SERIALPrimary Key:
Yes
Add additional columns:
first_name(VARCHAR(50), Not Null)last_name(VARCHAR(50), Not Null)email(VARCHAR(100), Unique, Not Null)phone_number(VARCHAR(15), Nullable)hire_date(DATE, Not Null)job_id(VARCHAR(10), Not Null)salary(DECIMAL(10,2), Not Null)
Click Save.
3. Adding Constraints to Employees Table
Types of Constraints:
Primary Key: Ensures unique and non-null employee_id.
Unique Constraint: Ensures no duplicate emails.
Check Constraint: Ensures salary is positive.
Foreign Key Constraint: Links job_id to
Jobstable.
Steps to Add Constraints:
In PgAdmin, expand Tables → Employees → Constraints.
Right-click Constraints → New Constraint.
Primary Key:
Name:
pk_employeeType:
Primary KeyColumns:
employee_idSave.
Unique Constraint:
Name:
unique_emailType:
UniqueColumns:
emailSave.
Check Constraint (Salary > 0):
Name:
check_salaryType:
CheckExpression:
salary > 0Save.
Foreign Key Constraint:
Name:
fk_jobType:
Foreign KeyColumn:
job_idReferences Table:
JobsReferences Column:
job_idOn Delete:
CASCADESave.
4. Creating Indexes on Employees Table
Indexes improve query performance.
Steps to Create an Index:
Expand Tables → Employees → Indexes.
Right-click Indexes → New Index.
General Tab:
Name:
idx_last_nameType:
B-tree
Columns Tab:
Select Column:
last_nameSort Order:
AscendingSave.
5. Setting Up Row-Level Security (RLS) Policy
Row-Level Security (RLS) controls access to table rows.
Steps to Enable RLS:
Expand Tables → Employees → RLS Policy.
Right-click RLS Policy → New RLS Policy.
General Tab:
Name:
policy_employeeCommand:
SELECT
Using Expression:
current_user = emailSave.
This ensures that employees can only view their own records.
6. Creating Rules for Automatic Data Modification
Rules allow automatic updates or inserts.
Steps to Create a Rule:
Expand Tables → Employees → Rules.
Right-click Rules → New Rule.
General Tab:
Name:
rule_no_deleteEvent:
DELETECondition:
trueInstead:
DO NOTHING
Save.
This prevents deletion of records.
7. Creating Triggers in Employees Table
Triggers execute a function when an event occurs.
Steps to Create a Trigger:
Expand Tables → Employees → Triggers.
Right-click Triggers → New Trigger.
General Tab:
Name:
before_insert_employeeEvents:
BEFORE INSERTTrigger Function:
audit_employee()
Save.
Steps to Create a Trigger Function:
Open Query Tool.
Run the SQL function:
sqlCopyEditCREATE OR REPLACE FUNCTION audit_employee() RETURNS TRIGGER AS $$ BEGIN INSERT INTO employee_audit (employee_id, action, timestamp) VALUES (NEW.employee_id, 'INSERT', NOW()); RETURN NEW; END; $$ LANGUAGE plpgsql;Click Execute.
Final Notes
Use Query Tool (
Ctrl + E) to test SQL queries.Use SELECT * FROM employees; to verify changes.
Backup data before applying constraints or triggers.
This tutorial covers creating tables, adding constraints, defining indexes, implementing row-level security, creating rules, and using triggers in PgAdmin.