PostgreSQL 18 database documentation - SQL, administration, and development
Comprehensive PostgreSQL 18 documentation covering SQL syntax, database administration, PL/pgSQL programming, and development best practices. Generated from official PostgreSQL documentation.
This skill should be activated when:
PostgreSQL is fully ACID-compliant:
Changes are logged before being applied to data files. This enables:
Server-side procedural language that:
Basic table creation (from official docs):
CREATE TABLE my_first_table (
first_column text,
second_column integer
);
Table with foreign key constraint (from official docs):
CREATE TABLE cities (
name varchar(80) PRIMARY KEY,
location point
);
CREATE TABLE weather (
city varchar(80) REFERENCES cities(name),
temp_lo int,
temp_hi int,
prcp real,
date date
);
Table inheritance (from official docs):
CREATE TABLE cities (
name text,
population real,
elevation int -- (in ft)
);
CREATE TABLE capitals (
state char(2) UNIQUE NOT NULL
) INHERITS (cities);
Basic SELECT (from official docs):
SELECT * FROM weather;
SELECT city, temp_lo, temp_hi, prcp, date FROM weather;
Computed columns with aliases (from official docs):
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
JOIN operations (from official docs):
-- Inner join
SELECT * FROM weather JOIN cities ON city = name;
-- Explicit column selection with qualified names
SELECT weather.city, weather.temp_lo, weather.temp_hi,
weather.prcp, weather.date, cities.location
FROM weather JOIN cities ON weather.city = cities.name;
Encapsulate complex queries (from official docs):
CREATE VIEW myview AS
SELECT name, temp_lo, temp_hi, prcp, date, location
FROM weather, cities
WHERE city = name;
SELECT * FROM myview;
Basic index creation (from official docs):
CREATE TABLE test1 (
id integer,
content varchar
);
CREATE INDEX test1_id_index ON test1 (id);
UPDATE statement (from official docs):
UPDATE weather
SET temp_hi = temp_hi - 2, temp_lo = temp_lo - 2
WHERE date > '1994-11-28';
INSERT statement (from official docs):
INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');
Basic transaction block (from official docs):
BEGIN;
UPDATE accounts SET balance = balance - 100.00
WHERE name = 'Alice';
-- ... more operations ...
COMMIT;
Using savepoints (from official docs):
BEGIN;
UPDATE accounts SET balance = balance - 100.00
WHERE name = 'Alice';
SAVEPOINT my_savepoint;
UPDATE accounts SET balance = balance + 100.00
WHERE name = 'Bob';
-- oops ... forget that and use Wally's account
ROLLBACK TO my_savepoint;
UPDATE accounts SET balance = balance + 100.00
WHERE name = 'Wally';
COMMIT;
Prepared statements in C (from official docs):
EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "INSERT INTO test1 VALUES(?, ?);";
EXEC SQL END DECLARE SECTION;
EXEC SQL PREPARE mystmt FROM :stmt;
EXEC SQL EXECUTE mystmt USING 42, 'foobar';
Creating a database (from official docs):
$ createdb mydb
Removing a database (from official docs):
$ dropdb mydb
Setting up shared libraries (from official docs):
LD_LIBRARY_PATH=/usr/local/pgsql/lib
export LD_LIBRARY_PATH
Parallel regression tests (from official docs):
make check # Against temporary installation
make installcheck # Against running server
make check-world # All test suites
This skill includes comprehensive documentation in references/:
Source: Official PostgreSQL 18 Documentation Confidence: Medium Pages: 46 Content:
Source: Official PostgreSQL 18 Documentation Confidence: Medium Pages: 1088 Content:
Source: Official PostgreSQL 18 Documentation Confidence: Medium Content:
references/getting_started.md for installation and basic SQLreferences/sql.md for complete SQL command syntax/docs/18/tutorial-select.html)Ensure data consistency by declaring foreign key constraints:
CREATE TABLE orders (
order_id serial PRIMARY KEY,
customer_id integer REFERENCES customers(id),
order_date date
);
Wrap related changes in a transaction block:
BEGIN;
-- Multiple related operations
UPDATE inventory SET quantity = quantity - 1 WHERE product_id = 123;
INSERT INTO order_items (order_id, product_id) VALUES (456, 123);
COMMIT;
Hide complexity behind a view interface:
CREATE VIEW active_customers AS
SELECT c.*, COUNT(o.order_id) as order_count
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE c.status = 'active'
GROUP BY c.id;
Create indexes on frequently queried columns:
CREATE INDEX idx_orders_customer ON orders(customer_id);
CREATE INDEX idx_orders_date ON orders(order_date);
ERROR: insert or update on table "weather" violates foreign key constraint
DETAIL: Key (city)=(Berkeley) is not present in table "cities".
Solution: Insert the referenced row first, or use ON DELETE/UPDATE actions.
After an error in a transaction, you must either:
ROLLBACK to abort and start freshROLLBACK TO savepoint to return to a known good stateOrganized documentation extracted from official PostgreSQL sources:
Add helper scripts for common automation tasks:
Add templates and examples:
To refresh this skill with updated documentation:
Primary Source: PostgreSQL 18 Official Documentation Documentation URL: https://www.postgresql.org/docs/18/ Source Confidence: Medium (official documentation) Last Updated: Generated from current documentation