PostgreSQL Syntax
Every task from the syntax reference, written for PostgreSQL, on one page. Use it as a quick lookup when you know what you want to do and need the PostgreSQL spelling. Rows marked standard use SQL that other databases share. For the other databases, see the topic pages linked from the SQL Syntax by Database.
How these were checked: every PostgreSQL and SQLite form on this page was run (PostgreSQL 18 and SQLite 3.54). MySQL, SQL Server and Oracle forms follow each database's current documentation (MySQL 8.4, SQL Server 2022 and later, Oracle AI Database 26ai); version notes such as "8.0.19+" mark features that older releases lack.
Querying
| Task | PostgreSQL | |
|---|---|---|
| First 10 rows | SELECT * FROM t ORDER BY id LIMIT 10; | standard |
| Skip 20 rows, take 10 | SELECT * FROM t ORDER BY id OFFSET 20 LIMIT 10; | standard |
| Case-insensitive match | SELECT * FROM t WHERE name ILIKE 'nav%'; | dialect |
| Rows with a missing value | SELECT * FROM t WHERE email IS NULL; | standard |
| Groups with a condition | SELECT city, count(*) FROM t GROUP BY city HAVING count(*) > 1; | standard |
| Full outer join | SELECT * FROM a FULL JOIN b ON b.id = a.id; | standard |
| Rows in one table but not another | SELECT id FROM a EXCEPT SELECT id FROM b; | standard |
| Quote a name with a space | SELECT "first name" FROM t; | standard |
All five databases side by side: Syntax: Querying.
Changing Data
| Task | PostgreSQL | |
|---|---|---|
| Insert several rows | INSERT INTO t (id, name) VALUES (1, 'a'), (2, 'b'); | standard |
| Get the new id back | INSERT INTO t (name) VALUES ('a') RETURNING id; | dialect |
| Update from another table | UPDATE t SET name = s.name FROM s WHERE s.id = t.id; | dialect |
| Insert or update (upsert) | INSERT INTO t (id, name) VALUES (1, 'a') ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name; | dialect |
| Delete all rows quickly | TRUNCATE TABLE t; | dialect |
| Copy rows into a new table | CREATE TABLE t2 AS SELECT * FROM t; | dialect |
| Load a CSV file | \copy t FROM 'file.csv' WITH (FORMAT csv, HEADER true) | dialect |
All five databases side by side: Syntax: Changing Data.
Tables and Keys
| Task | PostgreSQL | |
|---|---|---|
| Auto-numbered primary key | id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY | standard |
| Money column | fee_usd NUMERIC(8,2) | standard |
| Text column | name VARCHAR(40) | standard |
| True/false column | is_active BOOLEAN | standard |
| Foreign key with cascade | student_id INTEGER REFERENCES students (student_id) ON DELETE CASCADE | standard |
| Check constraint | CHECK (fee_usd > 0) | standard |
| Create table if missing | CREATE TABLE IF NOT EXISTS t (id INTEGER); | dialect |
| Add a column | ALTER TABLE t ADD COLUMN note TEXT; | standard |
| Rename a column | ALTER TABLE t RENAME COLUMN note TO memo; | standard |
| Create an index | CREATE INDEX t_name_idx ON t (name); | dialect |
| Create or replace a view | CREATE OR REPLACE VIEW v AS SELECT id FROM t; | dialect |
All five databases side by side: Syntax: Tables and Keys.
Functions
| Task | PostgreSQL | |
|---|---|---|
| Join text | first_name || ' ' || last_name | standard |
| Text length | length(name) | dialect |
| Part of a string | substring(name FROM 1 FOR 3) | standard |
| Replace NULL | COALESCE(email, 'none') | standard |
| Round to cents | round(x, 2) | dialect |
| Today's date | CURRENT_DATE | standard |
| Add 30 days | d + 30 | dialect |
| Days between dates | d2 - d1 | dialect |
| Year of a date | EXTRACT(YEAR FROM d) | standard |
| Format a date | to_char(d, 'YYYY-MM-DD') | dialect |
| Convert text to a number | CAST('450.00' AS NUMERIC) | standard |
| If / else | CASE WHEN x > 0 THEN 'yes' ELSE 'no' END | standard |
| Join values from many rows | string_agg(name, ', ' ORDER BY name) | standard |
All five databases side by side: Syntax: Functions.
Advanced SQL
| Task | PostgreSQL | |
|---|---|---|
| Common table expression | WITH x AS (SELECT 1 AS n) SELECT n FROM x; | standard |
| Recursive CTE | WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n; | standard |
| Rank rows | RANK() OVER (ORDER BY fee DESC) | standard |
| Running total | sum(x) OVER (ORDER BY d) | standard |
| Conditional count | count(*) FILTER (WHERE method = 'card') | standard |
| Subtotals | GROUP BY ROLLUP (a, b) | standard |
| Read a JSON field as text | data ->> 'name' | dialect |
| Full-text search | to_tsvector('english', body) @@ to_tsquery('english', 'join') | dialect |
| A function language | CREATE FUNCTION f() RETURNS int LANGUAGE plpgsql AS $$ BEGIN RETURN 1; END $$; | dialect |
All five databases side by side: Syntax: Advanced SQL.
Administration
| Task | PostgreSQL | |
|---|---|---|
| Start a transaction | BEGIN; | standard |
| Savepoint | SAVEPOINT s; ROLLBACK TO SAVEPOINT s; | standard |
| Set the isolation level | SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; | standard |
| Show a query plan | EXPLAIN (ANALYZE) SELECT ...; | dialect |
| Create a login | CREATE ROLE app LOGIN PASSWORD 'secret'; | dialect |
| Grant read access | GRANT SELECT ON payments TO app; | standard |
| Back up a database | pg_dump --format=custom --file=db.dump dbname | dialect |
| Parameter placeholder in app code | $1, $2 (node-postgres); %s (psycopg); ? (JDBC) | dialect |
| Server version | SELECT version(); | dialect |
All five databases side by side: Syntax: Administration.
Was this tutorial helpful?