CREATE DATABASE dbName;
CREATE TABLE tableName ( id serial PRIMARY KEY, name varchar(50) UNIQUE NOT NULL, dateCreated timestamp DEFAULT current_timestamp );
ALTER TABLE tableName ADD PRIMARY KEY (id);
CREATE UNIQUE INDEX indexName ON tableName (columnNames);
pg_dump dbName > dbName.sql
pg_dumpall > pgbackup.sql
psql -f script.sql databaseName
SELECT column FROM table WHERE column ~ 'foo.*';
SELECT columns FROM table LIMIT 10;
SELECT cols FROM table LIMIT 10 OFFSET 30;
PREPARE preparedInsert (int, varchar) AS INSERT INTO tableName (intColumn, charColumn) VALUES ($1, $2); EXECUTE preparedInsert (1,'a'); EXECUTE preparedInsert (2,'b'); DEALLOCATE preparedInsert;
CREATE OR REPLACE FUNCTION month (timestamp) RETURNS integer AS 'SELECT date_part(''month'', $1)::integer;' LANGUAGE 'sql';
VACUUM ANALYZE table;
REINDEX DATABASE dbName;
EXPLAIN SELECT * FROM table;
COPY destTable FROM '/tmp/somefile';
SHOW ALL;
GRANT ALL PRIVILEGES ON table TO username;
BEGIN TRANSACTION UPDATE accounts SET balance += 50 WHERE id = 1; COMMIT;
Basic SQL
SELECT * FROM table;
INSERT INTO table (column1,column2) VALUES (1, 'one');
UPDATE table SET foo = 'bar' WHERE id = 1;
DELETE FROM table WHERE id = 1;
http://www.petefreitag.com/cheatsheets/postgresql/