メインコンテンツまでスキップ

Installing PostgreSQL on Fedora

Let's talk about installing PostgreSQL on Fedora. PostgreSQL, often referred to as Postgres, is a powerful, open-source object-relational database system that boasts robustness, extensibility, and technical standards compliance. It is well-suited for complex applications and has a reputation for reliability, feature robustness, and performance.

Fedora, a Linux distribution known for its cutting-edge features and ease of use, provides a straightforward way to install PostgreSQL through its package management system. Whether you're a seasoned database administrator or a newcomer to the world of databases, this guide will walk you through the process of setting up PostgreSQL on your Fedora system.

Prerequisites

Before we begin, ensure that you have:

  • A Fedora system with administrative privileges.
  • Access to the terminal application.
  • An active internet connection to download the necessary packages.

Step 1: Update Your System

It's always a good practice to update your system packages to the latest versions. This ensures that you have the most recent package lists and software versions. Open your terminal and run the following command:

sudo dnf update -y

Step 2: Install PostgreSQL

Fedora's package manager, dnf, makes it easy to install PostgreSQL. Simply execute the following command:

sudo dnf install postgresql-server postgresql-contrib -y

This command installs both the PostgreSQL server (postgresql-server) and additional utilities and libraries (postgresql-contrib).

Step 3: Initialize the Database Cluster

After installation, you need to initialize the database cluster. This process creates the necessary directories and configuration files. Run the following command:

sudo postgresql-setup --initdb --unit postgresql

You should see an output similar to this, indicating a successful initialization:

Initializing database ... OK

Step 4: Start and Enable PostgreSQL Service

To start the PostgreSQL service and ensure it starts automatically on boot, use the following commands:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Verify that the service is running with:

sudo systemctl status postgresql

You should see active (running) in the output, indicating that PostgreSQL is up and running.

Step 5: Configure PostgreSQL

PostgreSQL is installed with a default configuration that should work for most use cases. However, you may want to tweak the settings to suit your specific needs. The main configuration files are located in /var/lib/pgsql/data/.

Adjusting Listen Addresses

Edit the postgresql.conf file to allow connections from other hosts (if necessary):

sudo nano /var/lib/pgsql/data/postgresql.conf

Find the line that specifies listen_addresses and change it to the desired setting, for example:

listen_addresses = 'localhost,192.168.1.100'

Save and close the file.

Configuring pg_hba.conf

The pg_hba.conf file controls which hosts are allowed to connect to the PostgreSQL server and how they are authenticated.

sudo nano /var/lib/pgsql/data/pg_hba.conf

Add or modify the lines according to your needs. For example, to allow password authentication for a specific IP range:

host    all             all             192.168.1.0/24        md5

Save and close the file, then apply the changes by reloading the PostgreSQL service:

sudo systemctl reload postgresql

Step 6: Create a New Database User and Database

By default, PostgreSQL creates a user named postgres with the role of a superuser. For security reasons, it's best practice to create a new user for your applications.

First, switch to the postgres user:

sudo -i -u postgres

Then, create a new user and database:

createuser --interactive
createdb mydatabase

Replace mydatabase with the name of your new database. When prompted, enter the password for the new user.

Step 7: Testing Your Installation

To test your PostgreSQL installation, use the psql command-line tool to connect to the database:

psql -U yourusername -d mydatabase

Replace yourusername with the username you created and mydatabase with your database name. You should be greeted with the PostgreSQL interactive terminal.

Step 8: Securing PostgreSQL

Security is a crucial aspect of database management. Here are a few tips to secure your PostgreSQL installation:

  • Regularly apply updates to PostgreSQL and the operating system.
  • Use strong passwords for database users.
  • Limit access to the PostgreSQL server using firewall rules.
  • Use SSL connections for remote access.
  • Regularly back up your databases.

You have now successfully installed and configured PostgreSQL on your Fedora system. With PostgreSQL's powerful features and Fedora's robust platform, you're well-equipped to handle a wide range of database-driven applications.