Skip to content

Instantly share code, notes, and snippets.

@jdecode
Last active March 19, 2024 07:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdecode/aad614641bdf0c7fc2f436eb7383f0fc to your computer and use it in GitHub Desktop.
Save jdecode/aad614641bdf0c7fc2f436eb7383f0fc to your computer and use it in GitHub Desktop.
psql-cli.md

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
  \dA[+]  [PATTERN]      list access methods
  \dAc[+] [AMPTRN [TYPEPTRN]]  list operator classes
  \dAf[+] [AMPTRN [TYPEPTRN]]  list operator families
  \dAo[+] [AMPTRN [OPFPTRN]]   list operators of operator families
  \dAp[+] [AMPTRN [OPFPTRN]]   list support functions of operator families
  \db[+]  [PATTERN]      list tablespaces
  \dc[S+] [PATTERN]      list conversions
  \dC[+]  [PATTERN]      list casts
  \dd[S]  [PATTERN]      show object descriptions not displayed elsewhere
  \dD[S+] [PATTERN]      list domains
  \ddp    [PATTERN]      list default privileges
  \dE[S+] [PATTERN]      list foreign tables
  \des[+] [PATTERN]      list foreign servers
  \det[+] [PATTERN]      list foreign tables
  \deu[+] [PATTERN]      list user mappings
  \dew[+] [PATTERN]      list foreign-data wrappers
  \df[anptw][S+] [FUNCPTRN [TYPEPTRN ...]]
                         list [only agg/normal/procedure/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[S+] [PATTERN]      list roles
  \dv[S+] [PATTERN]      list views
  \dx[+]  [PATTERN]      list extensions
  \dX     [PATTERN]      list extended statistics
  \dy[+]  [PATTERN]      list event triggers
  \l[+]   [PATTERN]      list databases
  \sf[+]  FUNCNAME       show a function's definition
  \sv[+]  VIEWNAME       show a view's definition
  \z      [PATTERN]      same as \dp
@jdecode
Copy link
Author

jdecode commented Nov 4, 2022

Some of the commands that I use often (for quick testing/peeking)

Create a new database with name as db_name

create database db_name;

Connect to DB named db_name

\connect db_name

Show databases

\l

Show tables [also called "relations"]

\d

@jdecode
Copy link
Author

jdecode commented Mar 16, 2023

Connect with local DB:

psql --host localhost --port 5432 --username postgres

@jdecode
Copy link
Author

jdecode commented Mar 16, 2023

Restore to newly created backup DB

pg_restore --host localhost --port 5432 --username postgres --dbname backup  --clean <PATH_TO_EXPORTED_BACKUP>

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