Skip to content

Instantly share code, notes, and snippets.

@joncode
Created May 24, 2015 20:28
Show Gist options
  • Save joncode/e17598cb88ccd4ede377 to your computer and use it in GitHub Desktop.
Save joncode/e17598cb88ccd4ede377 to your computer and use it in GitHub Desktop.
PG psql commants
General
\copyright show PostgreSQL usage and distribution terms
\g [FILE] or ; execute query (and send results to file or |pipe)
\gset [PREFIX] execute query and store results in psql variables
\h [NAME] help on syntax of SQL commands, * for all commands
\q quit psql
\watch [SEC] execute query every SEC seconds
Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\s [FILE] display history or save it to file
\w FILE write query buffer to file
Input/Output
\copy ... perform SQL COPY with data stream to the client host
\echo [STRING] write string to standard output
\i FILE execute commands from file
\ir FILE as \i, but relative to location of current script
\o [FILE] send all query results to file or |pipe
\qecho [STRING] write string to query output stream (see \o)
Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
\d[S+] NAME describe table, view, sequence, or index
\da[S] [PATTERN] list aggregates
\db[+] [PATTERN] list tablespaces
\dc[S+] [PATTERN] list conversions
\dC[+] [PATTERN] list casts
\dd[S] [PATTERN] show object descriptions not displayed elsewhere
2015-05-24T19:59:12Z app[postgres.16]: [TEAL] checkpoint starting: time
General
\copyright show PostgreSQL usage and distribution terms
\g [FILE] or ; execute query (and send results to file or |pipe)
\gset [PREFIX] execute query and store results in psql variables
\h [NAME] help on syntax of SQL commands, * for all commands
\q quit psql
\watch [SEC] execute query every SEC seconds
Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\s [FILE] display history or save it to file
\w FILE write query buffer to file
Input/Output
\copy ... perform SQL COPY with data stream to the client host
\echo [STRING] write string to standard output
\i FILE execute commands from file
\ir FILE as \i, but relative to location of current script
\o [FILE] send all query results to file or |pipe
\qecho [STRING] write string to query output stream (see \o)
Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
\d[S+] NAME describe table, view, sequence, or index
\da[S] [PATTERN] list aggregates
\db[+] [PATTERN] list tablespaces
\dc[S+] [PATTERN] list conversions
\dC[+] [PATTERN] list casts
\dd[S] [PATTERN] show object descriptions not displayed elsewhere
\ddp [PATTERN] list default privileges
\dD[S+] [PATTERN] list domains
\det[+] [PATTERN] list foreign tables
\des[+] [PATTERN] list foreign servers
\deu[+] [PATTERN] list user mappings
\dew[+] [PATTERN] list foreign-data wrappers
\df[antw][S+] [PATRN] list [only agg/normal/trigger/window] functions
\dF[+] [PATTERN] list text search configurations
\dFd[+] [PATTERN] list text search dictionaries
\dFp[+] [PATTERN] list text search parsers
\dFt[+] [PATTERN] list text search templates
\dg[+] [PATTERN] list roles
\di[S+] [PATTERN] list indexes
\dl list large objects, same as \lo_list
\dL[S+] [PATTERN] list procedural languages
\dm[S+] [PATTERN] list materialized views
\dn[S+] [PATTERN] list schemas
\do[S] [PATTERN] list operators
\dO[S+] [PATTERN] list collations
\dp [PATTERN] list table, view, and sequence access privileges
\drds [PATRN1 [PATRN2]] list per-database role settings
\ds[S+] [PATTERN] list sequences
\dt[S+] [PATTERN] list tables
\dT[S+] [PATTERN] list data types
\du[+] [PATTERN] list roles
\dv[S+] [PATTERN] list views
\dE[S+] [PATTERN] list foreign tables
\dx[+] [PATTERN] list extensions
\dy [PATTERN] list event triggers
\l[+] [PATTERN] list databases
\sf[+] FUNCNAME show a function's definition
\z [PATTERN] same as \dp
Formatting
\a toggle between unaligned and aligned output mode
\C [STRING] set table title, or unset if none
\f [STRING] show or set field separator for unaligned query output
\H toggle HTML output mode (currently off)
\pset NAME [VALUE] set table output option
(NAME := {format|border|expanded|fieldsep|fieldsep_zero|footer|null|
numericlocale|recordsep|recordsep_zero|tuples_only|title|tableattr|pager})
\t [on|off] show only rows (currently off)
\T [STRING] set HTML <table> tag attributes, or unset if none
\x [on|off|auto] toggle expanded output (currently off)
Connection
\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]
connect to new database (currently "drinkboard_dev")
\encoding [ENCODING] show or set client encoding
\password [USERNAME] securely change the password for a user
\conninfo display information about current connection
Operating System
\cd [DIR] change the current working directory
\setenv NAME [VALUE] set or unset environment variable
\timing [on|off] toggle timing of commands (currently off)
\! [COMMAND] execute command in shell or start interactive shell
Variables
\prompt [TEXT] NAME prompt user to set internal variable
\set [NAME [VALUE]] set internal variable, or list all if no parameters
\unset NAME unset (delete) internal variable
Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID large object operations
@joncode
Copy link
Author

joncode commented May 24, 2015

                                                                     List of data types

Schema | Name | Internal name | Size | Elements | Access privileges | Description
------------+-----------------------------+------------------+------+----------+-------------------+-------------------------------------------------------------------
pg_catalog | "any" | any | 4 | | |
pg_catalog | "char" | char | 1 | | | single character
pg_catalog | abstime | abstime | 4 | | | absolute, limited-range date and time (Unix system time)
pg_catalog | aclitem | aclitem | 12 | | | access control list
pg_catalog | anyarray | anyarray | var | | |
pg_catalog | anyelement | anyelement | 4 | | |
pg_catalog | anyenum | anyenum | 4 | | |
pg_catalog | anynonarray | anynonarray | 4 | | |
pg_catalog | anyrange | anyrange | var | | |
pg_catalog | bigint | int8 | 8 | | | ~18 digit integer, 8-byte storage
pg_catalog | bit | bit | var | | | fixed-length bit string
pg_catalog | bit varying | varbit | var | | | variable-length bit string
pg_catalog | boolean | bool | 1 | | | boolean, 'true'/'false'
pg_catalog | box | box | 32 | | | geometric box '(lower left,upper right)'
pg_catalog | bytea | bytea | var | | | variable-length string, binary values escaped
pg_catalog | character | bpchar | var | | | char(length), blank-padded string, fixed storage length
pg_catalog | character varying | varchar | var | | | varchar(length), non-blank-padded string, variable storage length
pg_catalog | cid | cid | 4 | | | command identifier type, sequence in transaction id
pg_catalog | cidr | cidr | var | | | network IP address/netmask, network address
pg_catalog | circle | circle | 24 | | | geometric circle '(center,radius)'
pg_catalog | cstring | cstring | var | | |
pg_catalog | date | date | 4 | | | date
pg_catalog | daterange | daterange | var | | | range of dates
pg_catalog | double precision | float8 | 8 | | | double-precision floating point number, 8-byte storage
pg_catalog | event_trigger | event_trigger | 4 | | |
pg_catalog | fdw_handler | fdw_handler | 4 | | |
pg_catalog | gtsvector | gtsvector | var | | | GiST index internal text representation for text search
pg_catalog | inet | inet | var | | | IP address/netmask, host address, netmask optional
pg_catalog | int2vector | int2vector | var | | | array of int2, used in system tables
pg_catalog | int4range | int4range | var | | | range of integers
pg_catalog | int8range | int8range | var | | | range of bigints
pg_catalog | integer | int4 | 4 | | | -2 billion to 2 billion integer, 4-byte storage
pg_catalog | internal | internal | 8 | | |
pg_catalog | interval | interval | 16 | | | @ , time interval
pg_catalog | json | json | var | | |
pg_catalog | language_handler | language_handler | 4 | | |
pg_catalog | line | line | 32 | | | geometric line (not implemented)
pg_catalog | lseg | lseg | 32 | | | geometric line segment '(pt1,pt2)'
pg_catalog | macaddr | macaddr | 6 | | | XX:XX:XX:XX:XX:XX, MAC address
pg_catalog | money | money | 8 | | | monetary amounts, $d,ddd.cc
pg_catalog | name | name | 64 | | | 63-byte type for storing system identifiers
pg_catalog | numeric | numeric | var | | | numeric(precision, decimal), arbitrary precision number
pg_catalog | numrange | numrange | var | | | range of numerics
pg_catalog | oid | oid | 4 | | | object identifier(oid), maximum 4 billion
pg_catalog | oidvector | oidvector | var | | | array of oids, used in system tables
pg_catalog | opaque | opaque | 4 | | |
pg_catalog | path | path | var | | | geometric path '(pt1,...)'
pg_catalog | pg_node_tree | pg_node_tree | var | | | string representing an internal node tree
pg_catalog | point | point | 16 | | | geometric point '(x, y)'
pg_catalog | polygon | polygon | var | | | geometric polygon '(pt1,...)'
pg_catalog | real | float4 | 4 | | | single-precision floating point number, 4-byte storage
pg_catalog | record | record | var | | |
pg_catalog | refcursor | refcursor | var | | | reference to cursor (portal name)
pg_catalog | regclass | regclass | 4 | | | registered class
pg_catalog | regconfig | regconfig | 4 | | | registered text search configuration
pg_catalog | regdictionary | regdictionary | 4 | | | registered text search dictionary
pg_catalog | regoper | regoper | 4 | | | registered operator
pg_catalog | regoperator | regoperator | 4 | | | registered operator (with args)
pg_catalog | regproc | regproc | 4 | | | registered procedure
pg_catalog | regprocedure | regprocedure | 4 | | | registered procedure (with args)
pg_catalog | regtype | regtype | 4 | | | registered type
pg_catalog | reltime | reltime | 4 | | | relative, limited-range time interval (Unix delta time)
pg_catalog | smallint | int2 | 2 | | | -32 thousand to 32 thousand, 2-byte storage
pg_catalog | smgr | smgr | 2 | | | storage manager
pg_catalog | text | text | var | | | variable-length string, no limit specified
pg_catalog | tid | tid | 6 | | | (block, offset), physical location of tuple
pg_catalog | time with time zone | timetz | 12 | | | time of day with time zone
pg_catalog | time without time zone | time | 8 | | | time of day
pg_catalog | timestamp with time zone | timestamptz | 8 | | | date and time with time zone
pg_catalog | timestamp without time zone | timestamp | 8 | | | date and time
pg_catalog | tinterval | tinterval | 12 | | | (abstime,abstime), time interval
pg_catalog | trigger | trigger | 4 | | |
pg_catalog | tsquery | tsquery | var | | | query representation for text search
pg_catalog | tsrange | tsrange | var | | | range of timestamps without time zone
pg_catalog | tstzrange | tstzrange | var | | | range of timestamps with time zone
pg_catalog | tsvector | tsvector | var | | | text representation for text search
pg_catalog | txid_snapshot | txid_snapshot | var | | | txid snapshot
pg_catalog | unknown | unknown | var | | |
pg_catalog | uuid | uuid | 16 | | | UUID datatype
pg_catalog | void | void | 4 | | |
pg_catalog | xid | xid | 4 | | | transaction id
pg_catalog | xml | xml | var | | | XML content

@joncode
Copy link
Author

joncode commented May 24, 2015

Listing TRIGGERS
You can list down all the triggers in the current database from pg_trigger table as follows:

SELECT * FROM pg_trigger;

http://www.tutorialspoint.com/postgresql/postgresql_triggers.htm

Listing Functions psql:
\df+

@joncode
Copy link
Author

joncode commented Jun 8, 2015

PSQL will not connect use this

postgres -D /usr/local/var/postgres

@joncode
Copy link
Author

joncode commented Jun 8, 2015

\x on

expanded display

@joncode
Copy link
Author

joncode commented Jun 15, 2015

HOW TO START THE SERVER

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting

@joncode
Copy link
Author

joncode commented Jun 24, 2015

brew info postgres

To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
postgres -D /usr/local/var/postgres

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment