Hello and welcome back to the course!
In the previous module, you successfully configured a complete web server stack, deploying a Django application with Gunicorn and Nginx, and even secured it with a TLS certificate. That's a significant milestone for any aspiring full-stack developer and system administrator.
Now, we shift our focus from the web-facing components to the data layer. While Django's default SQLite database is convenient for development, a production environment demands a more robust, scalable, and feature-rich database system. This is where PostgreSQL comes in.
This lesson kicks off our module on PostgreSQL Database Administration. Our goal is to install and initialize PostgreSQL on your Fedora system and start the service. By the end of this session, you will have a running PostgreSQL server, ready to host the database for your Django project.
1. Choosing the Right PostgreSQL Source
On Fedora, you have two primary methods for installing PostgreSQL:
- From Fedora's official repositories: This is the quickest method, but it provides a specific version of PostgreSQL that Fedora has "snapshotted" for that release.
- From the official PostgreSQL Global Development Group (PGDG) Yum repository: This is the method recommended by the PostgreSQL project itself. It gives you access to all supported PostgreSQL versions, allowing you to choose the exact one you need for your project and ensuring you get timely updates directly from the PostgreSQL developers.
As a system administrator, choosing your software source is a key decision. For our purposes, we will use the PGDG repository. This approach gives you more control and is standard practice for production server setups.
The official PostgreSQL website explains this distinction well.
Linux downloads (Red Hat family) - PostgreSQL
This page from the PostgreSQL website explains the difference between using your distribution's built-in packages and using the official PGDG Yum repository. It clarifies why the PGDG repository is the recommended choice.
Read the introductory paragraphs and the sections titled "PostgreSQL Yum Repository" and "Included in Distribution". Pay attention to the table that shows which PostgreSQL version comes with each Fedora release. This will help you understand the choice we are about to make.
2. Installing PostgreSQL via the PGDG Repository
We will now walk through the professional method for installing PostgreSQL. This involves first telling dnf to ignore Fedora's default PostgreSQL packages, then adding the official PGDG repository, and finally installing the server from that new source.
The following guide provides all the necessary commands. We will proceed step-by-step.
This comprehensive guide from 'if-not-true-then-false.com' details the installation of PostgreSQL on Fedora using the PGDG repository. We will follow its instructions closely.
For now, just familiarize yourself with the structure of the article. We will be using commands from sections 1.2, 1.3, 1.4, 2.1, and 2.4. You don't need to read it all at once; we will reference it as we go through the hands-on steps below.
Hands-On Task: Install, Initialize, and Start PostgreSQL
Open your terminal and follow these steps carefully. We will install PostgreSQL 16, which is a recent and stable version suitable for new projects.
Step 1: Exclude Fedora's Default PostgreSQL Packages
This is a crucial first step to prevent any conflicts between Fedora's packages and the ones we're about to install from the PGDG repository. These commands tell dnf to ignore any package named postgresql* from the default fedora and updates repositories.
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --save --setopt=fedora.exclude=postgresql*
sudo dnf config-manager --save --setopt=updates.exclude=postgresql*
These commands are found in section 1.2 of the "Install Postgresql on Fedora" guide.
Step 2: Add the PGDG Repository for Fedora
Now, we add the official PostgreSQL repository to your system's list of software sources.
# Since your system is Fedora 43, use the corresponding repository.
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/F-43-x86_64/pgdg-fedora-repo-latest.noarch.rpm
This command is found in section 1.3 of the guide.
Step 3: Install PostgreSQL Server and Client
With the repository configured, you can now install the PostgreSQL server and client packages. Notice how we specify the version number (16) in the package names.
sudo dnf install -y postgresql16-server postgresql16
This command is an adaptation of the examples in section 1.4 of the guide.
Step 4: Initialize the Database Cluster
Before you can use the database, you must initialize its data storage area on disk. This initdb command creates a new PostgreSQL "cluster," which is a collection of databases managed by a single server instance. It sets up template databases, configuration files, and the data directory (/var/lib/pgsql/16/data/).
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
This command is found in section 2.1 of the guide.
Step 5: Enable and Start the PostgreSQL Service
Finally, we use systemctl to start the PostgreSQL server immediately and enable it to launch automatically every time you boot your system.
sudo systemctl enable --now postgresql-16.service
The --now flag is a convenient shortcut that performs both enable and start actions in one go. This combined command is based on the separate start and enable commands shown in section 2.4 of the guide.
Step 6: Verify the Service Status
Confirm that the PostgreSQL server is active and running without any errors.
systemctl status postgresql-16.service
You should see output indicating the service is active (running), similar to how you checked the nginx and gunicorn services. Press q to exit the status view.
For a visual demonstration of a very similar process, you can watch the following video. While it uses slightly older versions (PostgreSQL 15 on Fedora 37), the core steps of adding the repository, installing, initializing, and starting the service are the same.
81. PostgreSQL DBA: How to install PostgreSQL 15 on Fedora 37
This video from r2schools provides a clear, step-by-step walkthrough of installing PostgreSQL on Fedora. Watching it will help reinforce the commands you just executed.
Watch from 01:55 to 05:04. This covers adding the repo, installing the packages with dnf, initializing the database, and finally enabling and starting the service with systemctl.
Conclusion
Congratulations! You have successfully installed and started a production-grade PostgreSQL database server on your Fedora system. You learned the critical difference between using distribution repositories and dedicated project repositories—a key piece of knowledge for any system administrator.
Key Takeaways:
- The PostgreSQL Global Development Group (PGDG) Yum repository is the recommended source for installing PostgreSQL on Fedora for server environments.
- It is best practice to exclude a distribution's default packages (
dnf config-manager --save --setopt=*.exclude=postgresql*) before adding an alternative repository to avoid conflicts. - The installation process follows a clear pattern: add repo -> install packages -> initialize database (
initdb) -> enable and start service (systemctl). - The service and commands for PGDG installations are version-specific (e.g.,
postgresql-16.service).
Preview of the Next Lesson:
Your PostgreSQL server is now running, but it's an empty slate. In the next lesson, we will connect to the database server for the first time. You will learn how to create a dedicated database and a user role specifically for your Django application, preparing it to be migrated from SQLite to PostgreSQL.