SQL

Structured Query Language

PostgreSQL


SQL Commands

User Management

Creating Users
 CREATE USER «name» WITH «option»;
   SUPERUSER | NOSUPERUSER
  INHERIT | NOINHERIT
  LOGIN | NOLOGIN
  PASSWORD 'password' | PASSWORD NULL
  VALID UNTIL 'timestamp'
  IN ROLE role_name [, ...]
  IN GROUP role_name [, ...]
Deleting Users
 DROP ROLE [IF EXISTS] «name»;
Setting Roles
 SET [SESSION | LOCAL] ROLE «role_name»
 RESET ROLE
 ALTER ROLE [ CURRENT_USER | SESSION_USER ] WITH «option»;

ALTER ROLE «name» RENAME TO «new_name»

Database Management

Creating a Database
 CREATE DATABASE «name»;

name: the name of the database to be created
username: the role name of the user who will own the new database or DEFAULT to use the default user
additional options:

 CREATE DATABASE «name» WITH «options»:

  OWNER = «user_name
  TEMPLATE = «template»
  ENCODING = «encoding»
  LOCALE = «locale»
  LC_COLLATE = «lc_collate»
  LC_CTYPE = «lc_ctype»
  TABLESPACE = «tablespace_name»
  ALLOW_CONNECTIONS = «allowconn»
  CONNECTION LIMIT = «connlimit»
  IS_TEMPLATE = «istemplate»
Deleting a Database
 DROP DATABASE «name»;
 DROP DATABASE IF EXISTS «name» WITH FORCE;

Table Management

Creating a Table
 CREATE TABLE «table name» (
  «column name» «data type»,
  «column name» «data type»,
  ...
  «column name» «data type»
);
Altering Tables
Adding Rows (Values)
 INSERT INTO «name» («column1», «column2», ...)
VALUES («value1», «value2», ...);

Queries

Select Query
 SELECT «column1», «column2»
FROM «table»;
WHERE clause
 SELECT «column»
FROM «table»
WHERE «column» = 'value';
ORDER BY
 SELECT «column»
FROM «table»
ORDER BY «column» [ DESC | ASC ]

Constraints

Check Constraints
Not-Null Constraints
Unique Constraints
Primary Keys
Foreign Keys

SQL Data Types

Name Aliases Description
bigint int8 signed eight-byte integer
bigserial serial8 autoincrementing eight-byte integer
bit [ (n) ] fixed-length bit string
bit varying [ (n) ] varbit [ (n) ] variable-length bit string
boolean bool logical Boolean (true/false)
box rectangular box on a plane
bytea binary data (“byte array”)
character [ (n) ] char [ (n) ] fixed-length character string
character varying [ (n) ] varchar [ (n) ] variable-length character string
cidr IPv4 or IPv6 network address
circle circle on a plane
date calendar date (year, month, day)
double precision float8 double precision floating-point number (8 bytes)
inet IPv4 or IPv6 host address
integer int, int4 signed four-byte integer
interval [ fields ] [ (p) ] time span
json textual JSON data
jsonb binary JSON data, decomposed
line infinite line on a plane
lseg line segment on a plane
macaddr MAC (Media Access Control) address
macaddr8 MAC (Media Access Control) address (EUI-64 format)
money currency amount
numeric [ (p, s) ] decimal [ (p, s) ] exact numeric of selectable precision
path geometric path on a plane
pg_lsn PostgreSQL Log Sequence Number
pg_snapshot user-level transaction ID snapshot
point geometric point on a plane
polygon closed geometric path on a plane
real float4 single precision floating-point number (4 bytes)
smallint int2 signed two-byte integer
smallserial serial2 autoincrementing two-byte integer
serial serial4 autoincrementing four-byte integer
text variable-length character string
time [ (p) ] [ without time zone ] time of day (no time zone)
time [ (p) ] with time zone timetz time of day, including time zone
timestamp [ (p) ] [ without time zone ] date and time (no time zone)
timestamp [ (p) ] with time zone timestamptz date and time, including time zone
tsquery text search query
tsvector text search document
txid_snapshot user-level transaction ID snapshot (deprecated; see pg_snapshot)
uuid universally unique identifier
xml XML data

SQL Operators

Logical Operators

Operator Description
ALL TRUE if all of the subquery values meet the condition.
AND TRUE if all the conditions separated by AND are TRUE.
ANY TRUE if any of the subquery values meet the condition.
BETWEEN TRUE if the operand is within the range of comparisons.
EXISTS TRUE if the subquery returns one or more records.
IN TRUE if the operand is equal to one of a list of expressions.
LIKE TRUE if the operand matches a pattern (accepts "wildcards").
NOT Displays a record of the conditions(s) is NOT TRUE.
OR TRUE if any of the conditions separated by OR is TRUE.
SOME TRUE if any of the subquery values meet the condition.

Arithmetic Operators

Operator Meaning Syntax
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b

Comparison Operators

Operator Meaning Syntax
= Equals a = b
!= Not equal to a != b
<> Not equal to a <> b
Greater than a > b
< Less than a < b
>= Greater or equal a >= b
<= Less or equal a <= b
!< Not less than a !< b
!> Not greater than a !> b