Postgresql Syntax
Postgresql Syntax
This chapter provides a list of the PostgreSQL SQL commands, followed by the precise syntax rules
for each of these commands. This set of commands is taken from the psql command-line tool. Now
that you have Postgres installed, open the psql as:
Using psql, you can generate the complete list of commands by using the \help command. For the
syntax of a specific command, use the following command:
ABORT
Abort the current transaction.
ALTER AGGREGATE
Change the definition of an aggregate function.
ALTER CONVERSION
Change the definition of a conversion.
ALTER DATABASE
Change a database specific parameter.
ALTER FUNCTION
Change the definition of a function.
ALTER GROUP
Change a user group.
ALTER INDEX
Change the definition of an index.
ALTER LANGUAGE
Change the definition of a procedural language.
ALTER OPERATOR
Change the definition of an operator.
ALTER SCHEMA
Change the definition of a schema.
ALTER TABLE
Change the definition of a table.
ALTER TABLESPACE
Change the definition of a tablespace.
ALTER TRIGGER
Change the definition of a trigger.
ALTER TYPE
Change the definition of a type.
ALTER USER
Change a database user account.
ALTER USER name [ [ WITH ] option [ ... ] ]
ALTER USER name RENAME TO new_name
ALTER USER name SET parameter { TO | = } { value | DEFAULT }
ALTER USER name RESET parameter
ANALYZE
Collect statistics about a database.
BEGIN
Start a transaction block.
CHECKPOINT
Force a transaction log checkpoint.
CHECKPOINT
CLOSE
Close a cursor.
CLOSE name
CLUSTER
Cluster a table according to an index.
COMMENT
Define or change the comment of an object.
COMMENT ON
{
TABLE object_name |
COLUMN table_name.column_name |
AGGREGATE agg_name (agg_type) |
CAST (source_type AS target_type) |
CONSTRAINT constraint_name ON table_name |
CONVERSION object_name |
DATABASE object_name |
DOMAIN object_name |
FUNCTION func_name (arg1_type, arg2_type, ...) |
INDEX object_name |
LARGE OBJECT large_object_oid |
OPERATOR op (left_operand_type, right_operand_type) |
OPERATOR CLASS object_name USING index_method |
[ PROCEDURAL ] LANGUAGE object_name |
RULE rule_name ON table_name |
SCHEMA object_name |
SEQUENCE object_name |
TRIGGER trigger_name ON table_name |
TYPE object_name |
VIEW object_name
} IS 'text'
COMMIT
Commit the current transaction.
COPY
Copy data between a file and a table.
CREATE AGGREGATE
Define a new aggregate function.
CREATE CAST
Define a new cast.
CREATE CAST (source_type AS target_type)
WITH FUNCTION func_name (arg_types)
[ AS ASSIGNMENT | AS IMPLICIT ]
CREATE CAST (source_type AS target_type)
WITHOUT FUNCTION
[ AS ASSIGNMENT | AS IMPLICIT ]
CREATE CONVERSION
Define a new conversion.
CREATE DATABASE
Create a new database.
CREATE DOMAIN
Define a new domain.
[ CONSTRAINT constraint_name ]
{ NOT NULL | NULL | CHECK (expression) }
CREATE FUNCTION
Define a new function.
CREATE INDEX
Define a new index.
CREATE LANGUAGE
Define a new procedural language.
CREATE OPERATOR
Define a new operator.
CREATE RULE
Define a new rewrite rule.
CREATE SCHEMA
Define a new schema.
CREATE SCHEMA schema_name
[ AUTHORIZATION username ] [ schema_element [ ... ] ]
CREATE SCHEMA AUTHORIZATION username
[ schema_element [ ... ] ]
CREATE SEQUENCE
Define a new sequence generator.
CREATE TABLE
Define a new table.
[ CONSTRAINT constraint_name ]
{ UNIQUE ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |
PRIMARY KEY ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |
CHECK ( expression ) |
FOREIGN KEY ( column_name [, ... ] )
REFERENCES ref_table [ ( ref_column [, ... ] ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE action ] [ ON UPDATE action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
CREATE TABLE AS
Define a new table from the results of a query.
CREATE TABLESPACE
Define a new tablespace.
CREATE TRIGGER
Define a new trigger.
CREATE TYPE
Define a new data type.
CREATE USER
Define a new database user account.
SYSID uid
| [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'
| CREATEDB | NOCREATEDB
| CREATEUSER | NOCREATEUSER
| IN GROUP group_name [, ...]
| VALID UNTIL 'abs_time'
CREATE VIEW
Define a new view.
DEALLOCATE
Deallocate a prepared statement.
DECLARE
Define a cursor.
DELETE
Delete rows of a table.
DROP AGGREGATE
Remove an aggregate function.
DROP CAST
Remove a cast.
DROP CONVERSION
Remove a conversion.
DROP DATABASE
Remove a database.
DROP DOMAIN
Remove a domain.
DROP FUNCTION
Remove a function.
DROP GROUP
Remove a user group.
DROP INDEX
Remove an index.
DROP INDEX name [, ...] [ CASCADE | RESTRICT ]
DROP LANGUAGE
Remove a procedural language.
DROP OPERATOR
Remove an operator.
DROP RULE
Remove a rewrite rule.
DROP SCHEMA
Remove a schema.
DROP SEQUENCE
Remove a sequence.
DROP TABLE
Remove a table.
DROP TABLESPACE
Remove a tablespace.
DROP TRIGGER
Remove a trigger.
DROP USER
Remove a database user account.
DROP VIEW
Remove a view.
END
Commit the current transaction.
EXECUTE
Execute a prepared statement.
EXPLAIN
Show the execution plan of a statement.
FETCH
Retrieve rows from a query using a cursor.
NEXT
PRIOR
FIRST
LAST
ABSOLUTE count
RELATIVE count
count
ALL
FORWARD
FORWARD count
FORWARD ALL
BACKWARD
BACKWARD count
BACKWARD ALL
GRANT
Define access privileges.
INSERT
Create new rows in a table.
LISTEN
Listen for a notification.
LISTEN name
LOAD
Load or reload a shared library file.
LOAD 'filename'
LOCK
Lock a table.
MOVE
Position a cursor.
MOVE [ direction { FROM | IN } ] cursor_name
NOTIFY
Generate a notification.
NOTIFY name
PREPARE
Prepare a statement for execution.
REINDEX
Rebuild indexes.
RELEASE SAVEPOINT
Destroy a previously defined savepoint.
RESET
Restore the value of a runtime parameter to the default value.
RESET name
RESET ALL
REVOKE
Remove access privileges.
ROLLBACK
Abort the current transaction.
ROLLBACK TO SAVEPOINT
Roll back to a savepoint.
SAVEPOINT
Define a new savepoint within the current transaction.
SAVEPOINT savepoint_name
SELECT
Retrieve rows from a table or view.
SELECT INTO
Define a new table from the results of a query.
SET
Change a runtime parameter.
SET CONSTRAINTS
Set constraint checking modes for the current transaction.
SET TRANSACTION
Set the characteristics of the current transaction.
SHOW
Show the value of a runtime parameter.
SHOW name
SHOW ALL
START TRANSACTION
Start a transaction block.
TRUNCATE
Empty a table.
UNLISTEN
Stop listening for a notification.
UNLISTEN { name | * }
UPDATE
Update rows of a table.
VACUUM
Garbage-collect and optionally analyze a database.