Categories: Uncategorized

Introduction to PostgreSQL: Advanced Open-source Database

PostgreSQL is a robust, open-source object-relational database management system (ORDBMS), commonly referred to as Postgres. PostgreSQL has been actively developed for more than 30 years, and is well known for its scalability, robustness, and conformance with standards. It is a popular option for developers, data engineers, and businesses worldwide because it offers sophisticated data types, complicated queries, full-text search, and transactional integrity.

Whether you’re developing a big data-driven platform or a little web application, PostgreSQL provides the adaptability and dependability required to manage challenging workloads with ease.

When to Use PostgreSQL?

Postgre SQL is a great fit in the following scenarios:

  • 1. You Need Complex Queries or Large Data Sets
  • Supports advanced joins, window functions, CTEs, and stored procedures.
  • Handles large-scale datasets efficiently with indexing, partitioning, and parallel processing.
  • 2. Data Integrity and Reliability Are Critical
  • Fully ACID compliant, ensuring reliable transactions.
  • Great for banking, finance, e-commerce, or any app where data accuracy is essential.
  • 3. You’re Working with JSON or Semi-Structured Data
  • Supports JSON & JSONB (binary JSON), enabling you to store and query semi-structured data.
  • Acts almost like a hybrid relational + NoSQL database.
  • 4. You Want an Open-Source, Extensible Database
  • 100% free and open source.
  • You can create custom data types, functions, and extensions (like PostGIS for geospatial data or TimescaleDB for time-series data).
  • 5. You Need Full-Text Search
  • Built-in support for full-text indexing and search, useful for content-heavy apps like blogs, CMS platforms, or product catalogs.
  • 6. You’re Deploying on Cloud or Need High Availability
  • Integrates well with cloud platforms (AWS RDS, GCP Cloud SQL, Azure).
  • Supports replication, failover, and point-in-time recovery.
  • 7. You Want Standards Compliance
  • PostgreSQL adheres closely to the SQL standard, which ensures long-term portability and maintainability.

When PostgreSQL Might Not Be Ideal to Use:

  • If you need horizontal scaling with millions of writes per second across nodes, a NoSQL solution like Cassandra may suit you better.
  • If you want a lightweight embedded database, SQLite might be better.
  • For small apps where you don’t need advanced features, a simpler database like MySQL might be easier to manage.

How to Install PostgreSQL on MacBook (macOS)

Option 1: Using Homebrew (Recommended)

Homebrew is the easiest and most popular way to install PostgreSQL on macOS. Do the following steps and use the commands in each step:

  • 1. Install Homebrew (if not already installed):
  • /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
  • 2. Update Homebrew and install PostgreSQL:
  • brew update
  • brew install PostgreSQL
  • 3. Start PostgreSQL service:
  • brew services start postgresql
  • 4. Verify the installation:
  • psql –version
  • PostgreSQL should now be running on the default port 5432. You can access the interactive terminal with the following command: psql postgres

Option 2: Using PostgreSQL Official Installer

You can also download a GUI installer from the official PostgreSQL website. Do the following steps:

  • Go to: https://www.postgresql.org/download/macosx/
  • Download the installer provided by EDB.
  • Follow the installation wizard (you’ll be able to set a password for the default ‘postgres’ user).
  • Note: This option installs pgAdmin as well, a graphical interface for managing your database.

How to Install PostgreSQL on Ubuntu?

Step-by-Step Guide for Installing PostgreSQL on Ubuntu. Do the steps below and use the commands in each step:

  • 1. Update the package list:
  • sudo apt update
  • 2. Install PostgreSQL and the additional modules:
  • sudo apt install postgresql postgresql-contrib
  • 3. Start the PostgreSQL service:
  • sudo systemctl start PostgreSQL
  • 4. Enable PostgreSQL to start on boot:
  • sudo systemctl enable postgresql
  • 5. Verify the installation:
  • psql –version
  • 6. Switch to the PostgreSQL default user:
  • sudo -i -u postgres
  • 7. Access PostgreSQL shell:
  • psql
  • Now you’re inside the PostgreSQL terminal. Type \q to exit.

Optional: Allow Remote Connections

To allow remote connections, do the steps below and use the following commands in each step:

  • 1. Edit postgresql.conf:
  • sudo nano /etc/postgresql/14/main/postgresql.conf
  • Set listen_addresses = ‘*’
  • 2. Edit pg_hba.conf:
  • sudo nano /etc/postgresql/14/main/pg_hba.conf
  • Add: host all all 0.0.0.0/0 md5
  • 3. Restart PostgreSQL:
  • sudo systemctl restart PostgreSQL
  • 4. Verify the software’s status to ensure installation and activation occurred without errors:
  • systemctl status postgresql

Creating Users and Databases:

For the following procedures, use the commands in each step:

Create a PostgreSQL User:

  • To create a new user:
  • sudo -u postgres createuser –interactive
  • You can also use SQL:
  • CREATE USER username WITH PASSWORD ‘yourpassword’;

Create a PostgreSQL DB:

  • Once a user is created, create a new database:
  • CREATE DATABASE mydatabase OWNER username;
  • Connect to it with:
  • psql -d mydatabase -U username

Using PostgreSQL Features:

For the following procedures, use the commands in each step:

PostgreSQL COALESCE

  • The COALESCE function returns the first non-null value in a list:
  • SELECT COALESCE (NULL, NULL, ‘default’, ‘another’) AS result;
  • — Returns ‘default’

PostgreSQL FOR UPDATE

  • Used to lock rows selected by a query until the transaction is completed:
  • BEGIN;
  • SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
  • — Perform updates here
  • COMMIT;

PL/pgSQL Language

  • PostgreSQL supports PL/pgSQL, a procedural language for writing functions and triggers:
  • CREATE FUNCTION add_numbers(a INT, b INT) RETURNS INT AS $$
  • BEGIN
  • RETURN a + b;
  • END;
  • $$ LANGUAGE plpgsql;

PostgreSQL with Docker & GIS

Postgres Docker GIS

  • PostgreSQL with PostGIS (a spatial database extender) using Docker:
  • docker run –name postgis -e POSTGRES_PASSWORD=mysecretpassword \
  • -d -p 5432:5432 postgis/postgis

PostgREST on Docker Hub

  • PostgREST is a tool that creates a REST API from any PostgreSQL database:
  • docker pull postgrest/postgrest

Note: You can visit Docker Hub site for more info: https://hub.docker.com/r/postgrest/postgrest

Conclusion:

PostgreSQL is an incredibly versatile and feature-rich database system. From installing it on your MacBook or Ubuntu machine to working with advanced SQL features and deploying GIS or RESTful APIs via Docker, PostgreSQL can support a wide range of applications. Whether you’re a developer or a data professional, PostgreSQL is a tool you can rely on.

References:

  1. PostgreSQL: macOS packages
  2. PostgreSQL: Linux downloads (Ubuntu)
  3. PostgreSQL: Documentation: 17: createuser
  4. PostgreSQL: Documentation: 17: CREATE DATABASE
  5. Installing on Docker | PostGIS
  6. PostgreSQL: Documentation: 17: Chapter 41. PL/pgSQL — SQL Procedural Language
  7. Ensuring Safe Data Modifications in PostgreSQL with SELECT FOR UPDATE – Stormatics
  8. PostgreSQL COALESCE | GeeksforGeeks

Terence Casquejo

Recent Posts

Introduction: What is Apache Hadoop?

What is Hadoop? Apache Hadoop is an open-source software framework designed for distributed storage and…

2 weeks ago

A Comprehensive Guide to Microsoft SQL Server

What is Microsoft SQL Server? Microsoft SQL Server is a relational database management system (RDBMS)…

1 month ago

Prometheus: Powerful Open-Source Monitoring & Alerting Toolkit

Welcome to the wild world of Prometheus monitoring! If you've ever wondered how to make…

1 month ago

A Comprehensive Guide to Zabbix: Installation, Configuration, and Monitoring

What is Zabbix? For seamless IT operations, Zabbix provides real-time monitoring, alerting, and visualization tools.…

2 months ago

Unlocking the Power of Splunk: A Comprehensive Guide

Splunk is a cutting-edge data analytics platform designed to search, monitor, and analyze machine-generated data…

2 months ago

Oracle Database: In-Depth Guide & FAQs (Simple, Clear and Includes “FAQs”)

What is Oracle Database? Oracle Database is a powerful, multi-model database management system developed by…

3 months ago

This website uses cookies.