What happens when you install Postgres 16 on Ubuntu

Installing software on Linux can often seem like a straightforward task, yet it involves a series of intricate processes that occur behind the scenes. When one embarks on the journey of installing PostgreSQL 16 on Ubuntu using the apt package manager, a fascinating orchestration of steps unfolds.

Step-by-Step Installation Process

The installation process can be broken down into several key steps:

  1. Update apt package lists: The command sudo apt update is executed to fetch the latest package lists from the configured repositories. This allows apt to understand which packages are available, their versions, and where to download them from. This step involves sending HTTP requests to repository servers, downloading package index files, and caching them locally.
  2. Resolve dependencies: Upon running sudo apt install postgresql-16, apt searches for the package in its lists. It then recursively identifies all dependencies required by PostgreSQL, creating a comprehensive dependency graph. Using sophisticated algorithms, apt resolves any conflicts to ensure a consistent set of package versions.
  3. Download packages: The necessary .deb package files for PostgreSQL and its dependencies are downloaded into the /var/cache/apt/archives/ directory. apt carefully determines the minimal set of packages needed for installation, thereby optimizing network transfer.
  4. Unpack and install: For each downloaded .deb package, apt performs the following actions:
    1. Extracts the package contents into a temporary directory using low-level filesystem tools like dpkg.
    2. Places the files in their appropriate locations within the filesystem, such as executables in /usr/bin, libraries in /usr/lib, and configuration files in /etc.
    3. Runs any pre-installation and post-installation scripts included in the package to perform necessary setup tasks.
    4. Updates the dpkg package database to record the installation status and metadata of the package.
  5. Set up the Postgres service: The post-installation scripts for the PostgreSQL package configure the server environment:
    1. Creates a dedicated postgres Linux user and group using useradd and groupadd.
    2. Initializes the data directory (default: /var/lib/postgresql/16/main/) with the initdb tool and sets permissions for the postgres user.
    3. Generates default configuration files, including postgresql.conf and pg_hba.conf.
    4. Sets up system catalog tables in a default postgres database.
    5. Configures PostgreSQL to start automatically on system boot using systemctl enable postgresql.service.
    6. Starts the PostgreSQL server by executing systemctl start postgresql.service, which spawns multiple processes, including the main server process Postmaster.
  6. Clean up: Finally, apt clears the local cache of downloaded .deb files from /var/cache/apt/archives/ to free up disk space after the installation is complete.

Final System State

Once the installation process concludes, several key processes are actively running:

  • postmaster: The main PostgreSQL server process, awaiting client connections.
  • checkpointer: Responsible for periodically flushing dirty data pages to disk.
  • background writer: Proactively writes dirty pages to disk.
  • walwriter: Writes Write Ahead Log (WAL) buffers to disk.
  • autovacuum launcher: Initiates autovacuum worker processes to remove old row versions.
  • stats collector: Gathers database statistics.
  • logical replication launcher: Manages logical replication slots.

These processes work in concert to handle client SQL queries, manage data, write buffers, facilitate replication, vacuum outdated data, and collect statistics. PostgreSQL is now poised to accept client connections through the local socket and is set to start automatically on future system reboots.

This intricate journey of executing a simple apt install postgres command reveals the complexity and elegance of package management on Linux, showcasing the seamless interaction of various components that make it all possible.

For a visual representation of this process, a flowchart can be found here.

Tech Optimizer
What happens when you install Postgres 16 on Ubuntu