Support Online
Skip to main content

PostgreSQL Installation

In this guide, you will learn how to install PostgreSQL on Ubuntu 22.04 server in a short time.
Post-installation role creation and database preparation are also covered.

Technical Summary

This guide explains how to install PostgreSQL on Ubuntu.
The goal is to get a working database into use quickly.
The steps consist of installation, verification, role and database creation.


Prerequisites

  • A server with Ubuntu 22.04 installed
  • User with authority sudo
  • At least 1 GB RAM

1. PostgreSQL Installation

First update the package list:

sudo apt update
  • This command refreshes the package information.

Install PostgreSQL and additional tools:


sudo apt install postgresql postgresql-contrib
  • This command installs the database server.

2. Verifying the Installation

  1. Check that the service is running:

sudo systemctl status postgresql
  • This command shows that the service is active.

  1. Verify the version:

sudo -u postgres psql -c "SELECT version();"
  • This command prints the installed PostgreSQL version.

3. Roles and Authentication

  • PostgreSQL manages access with a role system.
  • The default method is peer authentication.
  1. Switch to Postgres user:

sudo -i -u postgres
  • This command switches to the administrator account.

  1. Open the database console:

psql
  • This command starts the PostgreSQL shell.

To exit:


\q

4. Creating a New Role

  1. Create a custom user for the application:

sudo -u postgres createuser --interactive
  • This command creates an interactive role.

Do not grant superuser authority unless necessary.


5. Creating a New Database

  1. Create a database with the same name as the role:

sudo -u postgres createdb uygulama_db
  • This command prepares an empty database.

  1. Create the Linux user:

sudo adduser uygulama_db
  • This step is required for peer authentication.

Connect to database:


sudo -u uygulama_db psql
  • This command provides automatic login with the role.

Check the link:


\conninfo

7. Basic Security Settings

  1. Review the basic settings for production.

Open the configuration file:


sudo nano /etc/postgresql/*/main/postgresql.conf
  1. Edit the following settings:

max_connections = 100
shared_buffers = 256MB
effective_cache_size = 1GB
  • These settings improve memory usage.

  1. Apply the changes:

sudo systemctl restart postgresql

Frequently Asked Questions

  1. What should I do if the PostgreSQL service is not running? Run the systemctl start postgresql command.

  2. What causes peer authentication error? Linux username and role name do not match.

  3. Which PostgreSQL version is recommended? For new projects, PostgreSQL 15 or 16 is suitable.

  4. How to take a database backup? Use the command pg_dump database_name > backup.sql.


Result

With this guide, you quickly installed PostgreSQL on Ubuntu 22.04. Now you have a ready database for your applications.

You can safely use this structure on the GenixNode infrastructure