0% found this document useful (0 votes)
190 views

PostgreSQL Cheat Sheet

This document provides a cheat sheet of common PostgreSQL commands for creating and managing databases, tables, indexes, and performing queries, backups, imports, exports, and more. Key commands include CREATE DATABASE to create a new database, CREATE TABLE to define a table with an auto-incrementing id primary key, ALTER TABLE to add constraints like primary keys, COPY to import from files, EXPLAIN to view query plans, and GRANT to manage user permissions. Transactions, prepared statements, and functions are also summarized.

Uploaded by

Giova Rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views

PostgreSQL Cheat Sheet

This document provides a cheat sheet of common PostgreSQL commands for creating and managing databases, tables, indexes, and performing queries, backups, imports, exports, and more. Key commands include CREATE DATABASE to create a new database, CREATE TABLE to define a table with an auto-incrementing id primary key, ALTER TABLE to add constraints like primary keys, COPY to import from files, EXPLAIN to view query plans, and GRANT to manage user permissions. Transactions, prepared statements, and functions are also summarized.

Uploaded by

Giova Rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PostgreSQL Cheat Sheet

CREATE DATABASE
CREATE DATABASE dbName;

CREATE TABLE (with auto numbering integer id)


CREATE TABLE tableName (

id serial PRIMARY KEY,

name varchar(50) UNIQUE NOT NULL,

dateCreated timestamp DEFAULT current_timestamp

);

Add a primary key


ALTER TABLE tableName ADD PRIMARY KEY (id);

Create an INDEX
CREATE UNIQUE INDEX indexName ON tableName (columnNames);

Backup a database (command line)


pg_dump dbName > dbName.sql

Backup all databases (command line)


pg_dumpall > pgbackup.sql

Run a SQL script (command line)


psql -f script.sql databaseName

Search using a regular expression


SELECT column FROM table WHERE column ~ 'foo.*';

The first N records


SELECT columns FROM table LIMIT 10;

Pagination

1/3
SELECT cols FROM table LIMIT 10 OFFSET 30;

Prepared Statements
PREPARE preparedInsert (int, varchar) AS

INSERT INTO tableName (intColumn, charColumn) VALUES ($1, $2);

EXECUTE preparedInsert (1,'a');

EXECUTE preparedInsert (2,'b');

DEALLOCATE preparedInsert;

Create a Function
CREATE OR REPLACE FUNCTION month (timestamp) RETURNS integer
AS 'SELECT date_part(''month'', $1)::integer;'

LANGUAGE 'sql';

Table Maintenance
VACUUM ANALYZE table;

Reindex a database, table or index


REINDEX DATABASE dbName;

Show query plan


EXPLAIN SELECT * FROM table;

Import from a file


COPY destTable FROM '/tmp/somefile';

Show all runtime parameters


SHOW ALL;

Grant all permissions to a user


GRANT ALL PRIVILEGES ON table TO username;

Perform a transaction
BEGIN TRANSACTION
UPDATE accounts SET balance += 50 WHERE id = 1;

COMMIT;

Basic SQL

2/3
Get all columns and rows from a table
SELECT * FROM table;

Add a new row


INSERT INTO table (column1,column2)

VALUES (1, 'one');

Update a row
UPDATE table SET foo = 'bar' WHERE id = 1;

Delete a row
DELETE FROM table WHERE id = 1;

3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy