Introduction to Postgres
Postgres is one of the databases widely used in the market. It is a type of relational database. Basically it is a collection of records in an organized manner. There are different types of database models
- Hierarchy structure database model - Parent child like structure
- Network database model - It is a web of complexity
- Relational database model - Organized as rows and columns in a table
Data Integrity
There are four different integrities available in the database.
- Entity integrity - Uniqueness - Primary key ensures it.
- Referential integrity - Connection between tables/databases - Foreign key ensures it
- Domain integrity - Each field should follow some rules. For example, age should be a positive number
- User defined integrity - It depends on the application. For example, voting age should be 18. If not, it cannot update it in the table.
Installation
As I am using Mac, the installation steps are below
- Install homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install Postgres
brew install postgresql
- Start the postgres services
brew service start postgresql
- Verify Postgres installation
brew service list
- Access Postgres
psql postgres
Components installed during installation
When you install postgres, there are several processes occur to set up the database in the cluster.
1. Installation Phase
Binary installations such as postgres, psql are installed using the packages like homebrew, yum, apt etc. Configuration files such as postgresql.conf, pg_hba.conf, pg_ident.conf are configured by default.
2. Initiation Phase
The initdb
command runs in the initiation phase and prepares the environment for Postgres DB cluster. This will create
the following components.
1. Directory creation
/var/lib/postgresql/data
directory will create automatically if it does not exist. Subdirectories like base, global, pg_wal,
pg_stat
also create to organize the datafiles and metadata.
2. Configuration file creation
postgresql.conf
: This is the main configuration file
ps_hba.conf
: This is for client authentication
ps_ident.conf
: This helps to map OS users to DB users
3. Creation of system catalog
Core system catalog such as pg_class, pg_attribute, pg_database
are created to manage the database objects.
Essential metadata is initialized.
4. Default template setup
There are two default templates (template 0 and template 1).
template 0 | template 1 |
---|---|
This is pristine template used to create other databases | This is used to create databases by default. If it is broken, template 0 will be used to restore it |
You cannot modify template 0 (read only) | You can modify as per your need (custom extension, schemas and objects) |
5. Default databases
postgres
: A default database created for DBA
postgres
: A default user (super user) is created
template 0 & template 1
: Refer point # 4
6. WAL initialization
This ensures that the data recovery and durability. Writing Ahead Logging (WAL) is set up under pg_wal
.
Background processes
Once initialized, postgresql runs many different background processes for DBMS.
-
postmaster
process: This is the main server process that manages all the sub processes. -
WAL writer
process: It handles WAL updates -
Checkpoint
process: It helps to write the dirty pages to disk to maintain the data integrity -
Autovacuum
process: Automatically manages vacuum and analyse tasks -
Stats collector
process: Collects the stats for query optimization -
Archiver
process: Archives completed WAL segments when configured
Role Management
Roles are important in postgres as everything is handled using roles. Roles own the database objects and grant permission to perform operation like SELECT, UPDATE, DELETE, etc.
Roles can be two types
- Login role
- Non login role
If you need to perform any operation in postgres, you need to assign a role.
A role with login privilege is called user
.
Now satheesh
becomes user.
pandian
is a role because it doesn't have a login privilege. This role has a select, insert permission only to my_table.
It cannot access any other table, and also it doesn't do anything other than insert, select on my_table.
Key Points About Roles
- No Login by Default: Roles can only log in if the LOGIN attribute is set.
- Inheritance: Roles can inherit permissions from other roles using GRANT.
- Combining Roles: Users can belong to multiple roles, and their permissions will combine.
Examples for Roles
1. Basic Role Without Login
2. Role with Login (User Role)
CREATE ROLE pandian WITH LOGIN PASSWORD 'password';
GRANT SELECT, INSERT, UPDATE on EMPLOYEE TO pandian;
3. Role with Inheritance
CREATE ROLE employee;
GRANT SELECT on CUSTOMER TO employee;
CREATE ROLE manager;
GRANT INSERT, UPDATE ON CUSTOMER TO manager;
GRANT employee TO manager;
CREATE ROLE adhira WITH LOGIN PASSWORD 'password';
GRANT manager TO adhira;
Now adhira
can perform SELECT, INSERT,
and UPDATE operations on the CUSTOMER table because she inherits the permission from employee through a manager role.
4. Temporary Role