Object-Relational Database Management System
PostgreSQL (often called "Postgres") is a powerful, open-source object-relational database system with over 30 years of active development. It has earned a strong reputation for reliability, feature robustness, and performance.
Known for its SQL compliance, extensibility, and advanced features, PostgreSQL goes beyond traditional relational database functionality to provide robust support for complex data types, full-text search, geographic objects, and more. It can handle workloads ranging from small single-machine applications to large web services with many concurrent users.
-- Create a table with PostgreSQL data types CREATE TABLE products ( product_id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, price NUMERIC(10, 2) NOT NULL, description TEXT, weight REAL, in_stock BOOLEAN DEFAULT true, created_at TIMESTAMPTZ DEFAULT now(), tags TEXT[] ); -- Insert data with array INSERT INTO products (name, price, description, weight, tags) VALUES ('Ergonomic Keyboard', 129.99, 'Comfortable typing experience', 0.9, ARRAY['electronics', 'office', 'ergonomic']), ('Wireless Mouse', 45.50, 'High precision wireless mouse', 0.1, ARRAY['electronics', 'computer accessories']); -- Query with array operations SELECT * FROM products WHERE 'electronics' = ANY(tags) ORDER BY price DESC;
-- JSON data type and operations CREATE TABLE user_profiles ( user_id SERIAL PRIMARY KEY, name VARCHAR(100), preferences JSONB ); INSERT INTO user_profiles (name, preferences) VALUES ('Alice', '{"theme": "dark", "notifications": true, "language": "en"}'), ('Bob', '{"theme": "light", "notifications": false, "language": "fr"}'); -- Query JSON data SELECT name, preferences->'theme' AS theme FROM user_profiles WHERE preferences->>'language' = 'en'; -- Update JSON data UPDATE user_profiles SET preferences = preferences || '{"fontSize": "large"}'::jsonb WHERE name = 'Alice'; -- Common Table Expressions (CTE) WITH expensive_products AS ( SELECT * FROM products WHERE price > 100 ) SELECT * FROM expensive_products WHERE 'office' = ANY(tags);
-- Table inheritance CREATE TABLE vehicles ( vehicle_id SERIAL PRIMARY KEY, make VARCHAR(50), model VARCHAR(50), year INTEGER ); CREATE TABLE cars (INHERITS (vehicles)) ( doors INTEGER, body_type VARCHAR(20) ); -- Custom function CREATE OR REPLACE FUNCTION get_product_info(p_id INTEGER) RETURNS TABLE(name VARCHAR, price NUMERIC, tag_count INTEGER) AS $$ BEGIN RETURN QUERY SELECT p.name, p.price, array_length(p.tags, 1) AS tag_count FROM products p WHERE p.product_id = p_id; END; $$ LANGUAGE plpgsql; -- Using PostGIS extension for geographic data CREATE EXTENSION postgis; CREATE TABLE stores ( store_id SERIAL PRIMARY KEY, name VARCHAR(100), location GEOGRAPHY(POINT) ); INSERT INTO stores (name, location) VALUES ('Downtown Store', ST_MakePoint(-122.4194, 37.7749)::geography); -- Find stores within 10km SELECT name, ST_Distance(location, ST_MakePoint(-122.4, 37.8)::geography) AS distance FROM stores WHERE ST_DWithin(location, ST_MakePoint(-122.4, 37.8)::geography, 10000);
PostgreSQL is used in various contexts:
PostgreSQL has a rich development history:
PostgreSQL continues its development with annual major releases that focus on performance, features, and security while maintaining high backward compatibility.
Here are some excellent resources for learning PostgreSQL:
Technologies related to PostgreSQL: