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.
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.