Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

On-Premise Deployment of PostgreSQL

Introduction

Deploying PostgreSQL on-premises allows you to have complete control over your database environment. This tutorial will guide you through the process of installing and configuring PostgreSQL on a local server, including steps for securing, backing up, and maintaining your deployment.

1. System Requirements

Before installing PostgreSQL, ensure your server meets the following requirements:

  • Operating System: Linux (e.g., Ubuntu, CentOS), Windows, or macOS
  • Minimum 2 GB of RAM (4 GB recommended)
  • Minimum 10 GB of disk space
  • Network connectivity

2. Installing PostgreSQL

2.1. Installation on Ubuntu

Example Commands:

sudo apt update
sudo apt install postgresql postgresql-contrib

2.2. Installation on CentOS

Example Commands:

sudo yum install epel-release
sudo yum install postgresql-server postgresql-contrib
sudo postgresql-setup initdb

2.3. Installation on Windows

Example Steps:

1. Download the PostgreSQL installer from the official website.
2. Run the installer and follow the setup wizard.
3. Configure the installation directory, data directory, and password for the PostgreSQL superuser.

3. Configuring PostgreSQL

3.1. Starting the PostgreSQL Service

Example Commands:

# On Ubuntu
sudo systemctl start postgresql
sudo systemctl enable postgresql

# On CentOS
sudo systemctl start postgresql
sudo systemctl enable postgresql

3.2. Accessing the PostgreSQL Shell

Example Commands:

sudo -i -u postgres
psql

3.3. Creating a New Database and User

Example Commands:

CREATE DATABASE mydb;
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;

4. Securing PostgreSQL

Ensure your PostgreSQL deployment is secure by implementing the following best practices:

  • Use strong passwords for all database users.
  • Restrict access to the database by configuring the pg_hba.conf file.
  • Enable SSL/TLS to encrypt data in transit.
  • Regularly apply security patches and updates.

Example Configuration for pg_hba.conf:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

5. Backup and Recovery

Implement backup and recovery strategies to protect your data.

5.1. Creating a Backup

Example Command:

pg_dump mydb > mydb_backup.sql

5.2. Restoring from a Backup

Example Command:

psql mydb < mydb_backup.sql

6. Performance Tuning

Optimize the performance of your PostgreSQL database by adjusting the following settings in the postgresql.conf file:

  • shared_buffers: Allocate a portion of the system memory to PostgreSQL.
  • work_mem: Set the amount of memory to be used for internal sort operations and hash tables.
  • maintenance_work_mem: Allocate memory for maintenance operations like VACUUM.
  • effective_cache_size: Estimate the amount of memory available for caching.

Example Configuration for postgresql.conf:

shared_buffers = 1GB
work_mem = 64MB
maintenance_work_mem = 128MB
effective_cache_size = 4GB

7. Monitoring and Maintenance

Regularly monitor and maintain your PostgreSQL deployment to ensure its health and performance.

Conclusion

Deploying PostgreSQL on-premises provides full control over your database environment. By following the steps outlined in this tutorial, you can successfully install, configure, secure, and maintain a PostgreSQL instance on your local server.