Skip to content

Instantly share code, notes, and snippets.

@ma0c
Forked from Kartones/postgres-cheatsheet.md
Last active March 6, 2018 09:46
Show Gist options
  • Select an option

  • Save ma0c/c444fa967a570dcfbedcb96b4a0bd909 to your computer and use it in GitHub Desktop.

Select an option

Save ma0c/c444fa967a570dcfbedcb96b4a0bd909 to your computer and use it in GitHub Desktop.

Revisions

  1. Mauricio Collazos revised this gist Mar 6, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -119,4 +119,7 @@ Casting:
    ```bash
    $ echo "bind "^R" em-inc-search-prev" > $HOME/.editrc
    $ source $HOME/.editrc
    ```
    ```

    ## Comandos útiles
    - `ALTER USER user_name WITH PASSWORD 'new_password';` Cambia la contraseña de un usuario
  2. Mauricio Collazos revised this gist Apr 6, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@ La mayora de comandos `\d` soportan un parámetro adicional del `__schema__.name
    Relacionadas con usuarios:
    - `\du`: Lista de usuarios
    - `\du __username__`: Lista la información de un usuario si está presente
    - `create user __username__ with password 'password'`: Crea un usuario __username__ con contrasea password
    - `create role __test1__`: Crea un rol con un nombre de usuario específico
    - `create role __test2__ noinherit login password __passsword__;`: Crea un rol con un usuario y contraseña
    - `set role __test__;`: Cambia el rol de la sesión actual a `__test__`
  3. Mauricio Collazos revised this gist Feb 22, 2017. 1 changed file with 44 additions and 44 deletions.
    88 changes: 44 additions & 44 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,46 +1,46 @@

    ## PSQL

    Magic words:
    Palabras mágicas:
    ```bash
    psql -U postgres
    ```
    If run with `-E` flag, it will describe the underlaying queries of the `\` commands (cool for learning!).
    Si ejecutas con la bandera `-E`, describirá las consultas subyacentes que se realizan con los comandos `\` (genial para aprender).

    Most `\d` commands support additional param of `__schema__.name__` and accept wildcards like `*.*`
    La mayora de comandos `\d` soportan un parámetro adicional del `__schema__.name__` y aceptan comodines como `*.*`

    - `\q`: Quit/Exit
    - `\c __database__`: Connect to a database
    - `\d __table__`: Show table definition including triggers
    - `\dt *.*`: List tables from all schemas (if `*.*` is omitted will only show SEARCH_PATH ones)
    - `\l`: List databases
    - `\dn`: List schemas
    - `\df`: List functions
    - `\dv`: List views
    - `\df+ __function__` : Show function SQL code.
    - `\x`: Pretty-format query results instead of the not-so-useful ASCII tables
    - `\q`: Salir
    - `\c __database__`: Conectarse a una base de datos
    - `\d __table__`: Mostrar la definición de una tabla, incluyendo disparadores
    - `\dt *.*`: Lista todas las tablas de todos los esquemas (Si no se coloca *.* solamente se buscaran los pertenecientes ap SEARCH_PATH)
    - `\l`: Lista de las bases de datos
    - `\dn`: Lista de los esquemas
    - `\df`: Lista de las funciones
    - `\dv`: Lista de las vistas
    - `\df+ __function__` : Muestra el código SQL de una funcin
    - `\x`: Da una salida mas elaborada mostrando los resultados de la consulta

    User Related:
    - `\du`: List users
    - `\du __username__`: List a username if present.
    - `create role __test1__`: Create a role with an existing username.
    - `create role __test2__ noinherit login password __passsword__;`: Create a role with username and password.
    - `set role __test__;`: Change role for current session to `__test__`.
    - `grant __test2__ to __test1__;`: Allow `__test1__` to set its role as `__test2__`.
    Relacionadas con usuarios:
    - `\du`: Lista de usuarios
    - `\du __username__`: Lista la información de un usuario si está presente
    - `create role __test1__`: Crea un rol con un nombre de usuario específico
    - `create role __test2__ noinherit login password __passsword__;`: Crea un rol con un usuario y contraseña
    - `set role __test__;`: Cambia el rol de la sesión actual a `__test__`
    - `grant __test2__ to __test1__;`: Permite a `__test1__` definir su rol como `__test2__`

    ## Configuration
    ## Configuración

    - Service management commands:
    - Comandos de gestin de servicio:
    ```
    sudo service postgresql stop
    sudo service postgresql start
    sudo service postgresql restart
    ```

    - Changing verbosity & querying Postgres log:
    <br/>1) First edit the config file, set a decent verbosity, save and restart postgres:
    - Cambiar la verbosidad del log de Postgtgres:
     <br/>1) Primero edita el archivo de configuración, defone un log que te satisfaga, luego guarda y reinicia postgres:
    ```
    sudo vim /etc/postgresql/9.3/main/postgresql.conf
    sudo vim /etc/postgresql/9.x/main/postgresql.conf
    # Uncomment/Change inside:
    log_min_messages = debug5
    @@ -49,24 +49,24 @@ log_min_duration_statement = -1
    sudo service postgresql restart
    ```
    2) Now you will get tons of details of every statement, error, and even background tasks like VACUUMs
     2) Ahora puedes ver toneladas de detalles por cada sentencia, error e incluso tareas ejecutándose en segundo plano.
    ```
    tail -f /var/log/postgresql/postgresql-9.3-main.log
    tail -f /var/log/postgresql/postgresql-9.x-main.log
    ```
    3) How to add user who executed a PG statement to log (editing `postgresql.conf`):
     3) Se puede añadir quien ejecuto la sentencia en el log, editando el archivo `postgresql.conf`:
    ```
    log_line_prefix = '%t %u %d %a '
    ```


    ## Handy queries
    - `SELECT * FROM pg_proc WHERE proname='__procedurename__'`: List procedure/function
    - `SELECT * FROM pg_views WHERE viewname='__viewname__';`: List view (including the definition)
    - `SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));`: Show DB table space in use
    - `SELECT pg_size_pretty(pg_database_size('__database_name__'));`: Show DB space in use
    - `show statement_timeout;`: Show current user's statement timeout
    - `SELECT * FROM pg_indexes WHERE tablename='__table_name__' AND schemaname='__schema_name__';`: Show table indexes
    - Get all indexes from all tables of a schema:
    ## Consultas útiles
    - `SELECT * FROM pg_proc WHERE proname='__procedurename__'`: Lista los procedimientos/funciones
    - `SELECT * FROM pg_views WHERE viewname='__viewname__';`: Lista las vistas, incluidas las definiciones
    - `SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));`: Muestra la el espacio de tablas en uso.
    - `SELECT pg_size_pretty(pg_database_size('__database_name__'));`: Muestra el espacio de base de datos en uso
    - `show statement_timeout;`: Muestra el tiempo límite de finalización de la consulta.
    - `SELECT * FROM pg_indexes WHERE tablename='__table_name__' AND schemaname='__schema_name__';`: Muestra los índices de las tablas
    - Obtener todos los índices de todas las tablas de un esquema:
    ```sql
    SELECT
    t.relname AS table_name,
    @@ -89,18 +89,18 @@ ORDER BY
    t.relname,
    i.relname
    ```
    - Execution data:
    - Queries being executed at a certain DB:
    - Ejecutar datos:
     - Consutas a ser ejecutadas en una base de datos específica:
    ```sql
    SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query
    FROM pg_stat_activity
    WHERE datname='__database_name__';
    ```
    - Get all queries from all dbs waiting for data (might be hung):
    - Obtener todas las consultas a todas las bases de datos esperando por datos.
    ```sql
    SELECT * FROM pg_stat_activity WHERE waiting='t'
    ```
    - Currently running queries with process pid:
    - Consultas ejecutandose con su identificador de procesos:
    ```sql
    SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
    pg_stat_get_backend_activity(s.backendid) AS current_query
    @@ -109,12 +109,12 @@ FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;

    Casting:
    - `CAST (column AS type)` or `column::type`
    - `'__table_name__'::regclass::oid`: Get oid having a table name
    - `'__table_name__'::regclass::oid`: Obtener el identificador dado el nombre de la tabla


    ## Tools
    - [pg-top](http://ptop.projects.pgfoundry.org/): `top` for PG. `sudo apt-get install ptop` + `pg_top`
    - [Unix-like reverse search in psql](https://dba.stackexchange.com/questions/63453/is-there-a-psql-equivalent-of-bashs-reverse-search-history):
    ## Herramientas
    - [pg-top](http://ptop.projects.pgfoundry.org/): `top` para PG. `sudo apt-get install ptop` + `pg_top`
    - [Búsqueda inversa en PG como la de Unix](https://dba.stackexchange.com/questions/63453/is-there-a-psql-equivalent-of-bashs-reverse-search-history):
    ```bash
    $ echo "bind "^R" em-inc-search-prev" > $HOME/.editrc
    $ source $HOME/.editrc
  4. @Kartones Kartones revised this gist Aug 27, 2016. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -17,9 +17,17 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `\dn`: List schemas
    - `\df`: List functions
    - `\dv`: List views
    - `\df+ __function` : Show function SQL code.
    - `\df+ __function__` : Show function SQL code.
    - `\x`: Pretty-format query results instead of the not-so-useful ASCII tables

    User Related:
    - `\du`: List users
    - `\du __username__`: List a username if present.
    - `create role __test1__`: Create a role with an existing username.
    - `create role __test2__ noinherit login password __passsword__;`: Create a role with username and password.
    - `set role __test__;`: Change role for current session to `__test__`.
    - `grant __test2__ to __test1__;`: Allow `__test1__` to set its role as `__test2__`.

    ## Configuration

    - Service management commands:
  5. @Kartones Kartones revised this gist Mar 2, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -105,4 +105,9 @@ Casting:


    ## Tools
    - [pg-top](http://ptop.projects.pgfoundry.org/): `top` for PG. `sudo apt-get install ptop` + `pg_top`
    - [pg-top](http://ptop.projects.pgfoundry.org/): `top` for PG. `sudo apt-get install ptop` + `pg_top`
    - [Unix-like reverse search in psql](https://dba.stackexchange.com/questions/63453/is-there-a-psql-equivalent-of-bashs-reverse-search-history):
    ```bash
    $ echo "bind "^R" em-inc-search-prev" > $HOME/.editrc
    $ source $HOME/.editrc
    ```
  6. @Kartones Kartones revised this gist Feb 18, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -35,8 +35,9 @@ sudo service postgresql restart
    sudo vim /etc/postgresql/9.3/main/postgresql.conf
    # Uncomment/Change inside:
    log_min_messages = debug2
    log_min_error_statement = debug2
    log_min_messages = debug5
    log_min_error_statement = debug5
    log_min_duration_statement = -1
    sudo service postgresql restart
    ```
  7. @Kartones Kartones revised this gist Feb 11, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,8 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `\dn`: List schemas
    - `\df`: List functions
    - `\dv`: List views
    - `\df+ __function` : Show function SQL code. `\x` before pretty-formats it
    - `\df+ __function` : Show function SQL code.
    - `\x`: Pretty-format query results instead of the not-so-useful ASCII tables

    ## Configuration

  8. @Kartones Kartones revised this gist Feb 11, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -82,7 +82,9 @@ ORDER BY
    - Execution data:
    - Queries being executed at a certain DB:
    ```sql
    SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query FROM pg_stat_activity WHERE datname='__database_name__';
    SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query
    FROM pg_stat_activity
    WHERE datname='__database_name__';
    ```
    - Get all queries from all dbs waiting for data (might be hung):
    ```sql
  9. @Kartones Kartones revised this gist Feb 11, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -82,7 +82,7 @@ ORDER BY
    - Execution data:
    - Queries being executed at a certain DB:
    ```sql
    SELECT pid, datname, waiting, state, query FROM pg_stat_activity WHERE datname='__database_name__';
    SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query FROM pg_stat_activity WHERE datname='__database_name__';
    ```
    - Get all queries from all dbs waiting for data (might be hung):
    ```sql
  10. @Kartones Kartones revised this gist Jan 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ sudo service postgresql restart
    ```

    - Changing verbosity & querying Postgres log:
    1) First edit the config file, set a decent verbosity, save and restart postgres:
    <br/>1) First edit the config file, set a decent verbosity, save and restart postgres:
    ```
    sudo vim /etc/postgresql/9.3/main/postgresql.conf
  11. @Kartones Kartones revised this gist Jan 27, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ sudo service postgresql restart
    ```

    - Changing verbosity & querying Postgres log:
    - First edit the config file, set a decent verbosity, save and restart postgres:
    1) First edit the config file, set a decent verbosity, save and restart postgres:
    ```
    sudo vim /etc/postgresql/9.3/main/postgresql.conf
    @@ -39,11 +39,11 @@ log_min_error_statement = debug2
    sudo service postgresql restart
    ```
    - Now you will get tons of details of every statement, error, and even background tasks like VACUUMs
    2) Now you will get tons of details of every statement, error, and even background tasks like VACUUMs
    ```
    tail -f /var/log/postgresql/postgresql-9.3-main.log
    ```
    - How to add user who executed a PG statement to log (editing `postgresql.conf`):
    3) How to add user who executed a PG statement to log (editing `postgresql.conf`):
    ```
    log_line_prefix = '%t %u %d %a '
    ```
  12. @Kartones Kartones revised this gist Jan 27, 2016. 1 changed file with 11 additions and 4 deletions.
    15 changes: 11 additions & 4 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -21,8 +21,15 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi

    ## Configuration

    Changing verbosity & querying Postgres log:
    - First edit the config file, set a decent verbosity, save and restart postgres:
    - Service management commands:
    ```
    sudo service postgresql stop
    sudo service postgresql start
    sudo service postgresql restart
    ```

    - Changing verbosity & querying Postgres log:
    - First edit the config file, set a decent verbosity, save and restart postgres:
    ```
    sudo vim /etc/postgresql/9.3/main/postgresql.conf
    @@ -32,11 +39,11 @@ log_min_error_statement = debug2
    sudo service postgresql restart
    ```
    - Now you will get tons of details of every statement, error, and even background tasks like VACUUMs
    - Now you will get tons of details of every statement, error, and even background tasks like VACUUMs
    ```
    tail -f /var/log/postgresql/postgresql-9.3-main.log
    ```
    - How to add user who executed a PG statement to log (editing `postgresql.conf`):
    - How to add user who executed a PG statement to log (editing `postgresql.conf`):
    ```
    log_line_prefix = '%t %u %d %a '
    ```
  13. @Kartones Kartones revised this gist Jan 27, 2016. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -51,38 +51,38 @@ log_line_prefix = '%t %u %d %a '
    - `SELECT * FROM pg_indexes WHERE tablename='__table_name__' AND schemaname='__schema_name__';`: Show table indexes
    - Get all indexes from all tables of a schema:
    ```sql
    select
    t.relname as table_name,
    i.relname as index_name,
    a.attname as column_name
    from
    SELECT
    t.relname AS table_name,
    i.relname AS index_name,
    a.attname AS column_name
    FROM
    pg_class t,
    pg_class i,
    pg_index ix,
    pg_attribute a,
    pg_namespace n
    where
    WHERE
    t.oid = ix.indrelid
    and i.oid = ix.indexrelid
    and a.attrelid = t.oid
    and a.attnum = ANY(ix.indkey)
    and t.relnamespace = n.oid
    and n.nspname = 'kartones'
    order by
    AND i.oid = ix.indexrelid
    AND a.attrelid = t.oid
    AND a.attnum = ANY(ix.indkey)
    AND t.relnamespace = n.oid
    AND n.nspname = 'kartones'
    ORDER BY
    t.relname,
    i.relname
    ```
    - Execution data:
    - Queries being executed at a certain DB:
    ```
    ```sql
    SELECT pid, datname, waiting, state, query FROM pg_stat_activity WHERE datname='__database_name__';
    ```
    - Get all queries from all dbs waiting for data (might be hung):
    ```
    ```sql
    SELECT * FROM pg_stat_activity WHERE waiting='t'
    ```
    - Currently running queries with process pid:
    ```
    ```sql
    SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
    pg_stat_get_backend_activity(s.backendid) AS current_query
    FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
  14. @Kartones Kartones revised this gist Jan 27, 2016. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,6 @@ log_line_prefix = '%t %u %d %a '
    - `SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));`: Show DB table space in use
    - `SELECT pg_size_pretty(pg_database_size('__database_name__'));`: Show DB space in use
    - `show statement_timeout;`: Show current user's statement timeout
    - `SELECT pid, datname, waiting, state, query FROM pg_stat_activity WHERE datname='__database_name__';`: Show queries being executed at a certain DB. Can also display query time, etc.
    - `SELECT * FROM pg_indexes WHERE tablename='__table_name__' AND schemaname='__schema_name__';`: Show table indexes
    - Get all indexes from all tables of a schema:
    ```sql
    @@ -73,8 +72,16 @@ order by
    t.relname,
    i.relname
    ```
    - `select * from pg_stat_activity where waiting='t'`: Get all queries waiting for data (that might be hung)
    - Get all currently executing queries:
    - Execution data:
    - Queries being executed at a certain DB:
    ```
    SELECT pid, datname, waiting, state, query FROM pg_stat_activity WHERE datname='__database_name__';
    ```
    - Get all queries from all dbs waiting for data (might be hung):
    ```
    SELECT * FROM pg_stat_activity WHERE waiting='t'
    ```
    - Currently running queries with process pid:
    ```
    SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
    pg_stat_get_backend_activity(s.backendid) AS current_query
  15. @Kartones Kartones revised this gist Jan 27, 2016. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@

    ## PSQL

    Magic words:
    ```bash
    psql -U postgres
    @@ -16,9 +19,7 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `\dv`: List views
    - `\df+ __function` : Show function SQL code. `\x` before pretty-formats it

    Casting:
    - `CAST (column AS type)` or `column::type`
    - `'__table_name__'::regclass::oid`: Get oid having a table name
    ## Configuration

    Changing verbosity & querying Postgres log:
    - First edit the config file, set a decent verbosity, save and restart postgres:
    @@ -41,7 +42,7 @@ log_line_prefix = '%t %u %d %a '
    ```


    SQL queries:
    ## Handy queries
    - `SELECT * FROM pg_proc WHERE proname='__procedurename__'`: List procedure/function
    - `SELECT * FROM pg_views WHERE viewname='__viewname__';`: List view (including the definition)
    - `SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));`: Show DB table space in use
    @@ -78,4 +79,12 @@ order by
    SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
    pg_stat_get_backend_activity(s.backendid) AS current_query
    FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
    ```
    ```

    Casting:
    - `CAST (column AS type)` or `column::type`
    - `'__table_name__'::regclass::oid`: Get oid having a table name


    ## Tools
    - [pg-top](http://ptop.projects.pgfoundry.org/): `top` for PG. `sudo apt-get install ptop` + `pg_top`
  16. @Kartones Kartones revised this gist Oct 23, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -75,6 +75,7 @@ order by
    - `select * from pg_stat_activity where waiting='t'`: Get all queries waiting for data (that might be hung)
    - Get all currently executing queries:
    ```
    SELECT pg_stat_get_backend_pid(s.backendid) AS procpid, pg_stat_get_backend_activity(s.backendid) AS current_query
    SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
    pg_stat_get_backend_activity(s.backendid) AS current_query
    FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
    ```
  17. @Kartones Kartones revised this gist Oct 23, 2015. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -72,4 +72,9 @@ order by
    t.relname,
    i.relname
    ```
    - `select * from pg_stat_activity where waiting='t'`: Get all queries waiting for data (that might be hung)
    - `select * from pg_stat_activity where waiting='t'`: Get all queries waiting for data (that might be hung)
    - Get all currently executing queries:
    ```
    SELECT pg_stat_get_backend_pid(s.backendid) AS procpid, pg_stat_get_backend_activity(s.backendid) AS current_query
    FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
    ```
  18. @Kartones Kartones revised this gist Oct 23, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,11 @@ sudo service postgresql restart
    ```
    tail -f /var/log/postgresql/postgresql-9.3-main.log
    ```
    - How to add user who executed a PG statement to log (editing `postgresql.conf`):
    ```
    log_line_prefix = '%t %u %d %a '
    ```


    SQL queries:
    - `SELECT * FROM pg_proc WHERE proname='__procedurename__'`: List procedure/function
  19. @Kartones Kartones revised this gist Oct 23, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -66,4 +66,5 @@ where
    order by
    t.relname,
    i.relname
    ```
    ```
    - `select * from pg_stat_activity where waiting='t'`: Get all queries waiting for data (that might be hung)
  20. @Kartones Kartones revised this gist Oct 19, 2015. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,22 @@ Casting:
    - `CAST (column AS type)` or `column::type`
    - `'__table_name__'::regclass::oid`: Get oid having a table name

    Changing verbosity & querying Postgres log:
    - First edit the config file, set a decent verbosity, save and restart postgres:
    ```
    sudo vim /etc/postgresql/9.3/main/postgresql.conf
    # Uncomment/Change inside:
    log_min_messages = debug2
    log_min_error_statement = debug2
    sudo service postgresql restart
    ```
    - Now you will get tons of details of every statement, error, and even background tasks like VACUUMs
    ```
    tail -f /var/log/postgresql/postgresql-9.3-main.log
    ```

    SQL queries:
    - `SELECT * FROM pg_proc WHERE proname='__procedurename__'`: List procedure/function
    - `SELECT * FROM pg_views WHERE viewname='__viewname__';`: List view (including the definition)
  21. @Kartones Kartones revised this gist Sep 7, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ Magic words:
    ```bash
    psql -U postgres
    ```
    If run with `-E` flag, it will describe the underlaying queries of the `\` commands (cool for learning!).

    Most `\d` commands support additional param of `__schema__.name__` and accept wildcards like `*.*`

  22. @Kartones Kartones revised this gist Jul 14, 2015. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -14,18 +14,19 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `\df`: List functions
    - `\dv`: List views
    - `\df+ __function` : Show function SQL code. `\x` before pretty-formats it

    Casting:
    - `CAST (column AS type)` or `column::type`
    - `'__table_name__'::regclass::oid`: Get oid having a table name

    SQL queries:
    - `SELECT * FROM pg_proc WHERE proname='__procedurename__'`: List procedure/function
    - `SELECT * FROM pg_views WHERE viewname='__viewname__';`: List view (including the definition)
    - `SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));`: Show DB table space in use
    - `SELECT pg_size_pretty(pg_database_size('__database_name__'));`: Show DB space in use
    - `show statement_timeout;`: Show current user's statement timeout
    - `SELECT pid, datname, waiting, state, query FROM pg_stat_activity WHERE datname='__database_name__';`: Show queries being executed at a certain DB. Can also display query time, etc.
    - `SELECT * FROM pg_indexes WHERE tablename='__table_name__' AND schemaname='__schema_name__';`: Show table indexes

    Casting:
    - `CAST (column AS type)` or `column::type`

    SQL queries:
    - Get all indexes from all tables of a schema:
    ```sql
    select
  23. @Kartones Kartones revised this gist Jul 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `SELECT pg_size_pretty(pg_database_size('__database_name__'));`: Show DB space in use
    - `show statement_timeout;`: Show current user's statement timeout
    - `SELECT pid, datname, waiting, state, query FROM pg_stat_activity WHERE datname='__database_name__';`: Show queries being executed at a certain DB. Can also display query time, etc.
    - `SELECT * FROM pg_indexes WHERE tablename='__table_name__';`: Show table indexes
    - `SELECT * FROM pg_indexes WHERE tablename='__table_name__' AND schemaname='__schema_name__';`: Show table indexes

    Casting:
    - `CAST (column AS type)` or `column::type`
  24. @Kartones Kartones revised this gist Jul 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ Casting:
    - `CAST (column AS type)` or `column::type`

    SQL queries:
    - Get all indexes from all tables of a scheam:
    - Get all indexes from all tables of a schema:
    ```sql
    select
    t.relname as table_name,
  25. @Kartones Kartones revised this gist Jul 14, 2015. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -24,3 +24,28 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi

    Casting:
    - `CAST (column AS type)` or `column::type`

    SQL queries:
    - Get all indexes from all tables of a scheam:
    ```sql
    select
    t.relname as table_name,
    i.relname as index_name,
    a.attname as column_name
    from
    pg_class t,
    pg_class i,
    pg_index ix,
    pg_attribute a,
    pg_namespace n
    where
    t.oid = ix.indrelid
    and i.oid = ix.indexrelid
    and a.attrelid = t.oid
    and a.attnum = ANY(ix.indkey)
    and t.relnamespace = n.oid
    and n.nspname = 'kartones'
    order by
    t.relname,
    i.relname
    ```
  26. @Kartones Kartones revised this gist Jul 14, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,7 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `SELECT pg_size_pretty(pg_database_size('__database_name__'));`: Show DB space in use
    - `show statement_timeout;`: Show current user's statement timeout
    - `SELECT pid, datname, waiting, state, query FROM pg_stat_activity WHERE datname='__database_name__';`: Show queries being executed at a certain DB. Can also display query time, etc.
    - `SELECT * FROM pg_indexes WHERE tablename='__table_name__';`: Show table indexes

    Casting:
    - `CAST (column AS type)` or `column::type`
  27. @Kartones Kartones revised this gist May 29, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,7 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));`: Show DB table space in use
    - `SELECT pg_size_pretty(pg_database_size('__database_name__'));`: Show DB space in use
    - `show statement_timeout;`: Show current user's statement timeout
    - `SELECT pid, datname, waiting, state, query FROM pg_stat_activity WHERE datname='__database_name__';`: Show queries being executed at a certain DB. Can also display query time, etc.

    Casting:
    - `CAST (column AS type)` or `column::type`
  28. @Kartones Kartones revised this gist Apr 14, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,7 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `SELECT * FROM pg_views WHERE viewname='__viewname__';`: List view (including the definition)
    - `SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));`: Show DB table space in use
    - `SELECT pg_size_pretty(pg_database_size('__database_name__'));`: Show DB space in use
    - `show statement_timeout;`: Show current user's statement timeout

    Casting:
    - `CAST (column AS type)` or `column::type`
  29. @Kartones Kartones revised this gist Sep 1, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -18,3 +18,6 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `SELECT * FROM pg_views WHERE viewname='__viewname__';`: List view (including the definition)
    - `SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));`: Show DB table space in use
    - `SELECT pg_size_pretty(pg_database_size('__database_name__'));`: Show DB space in use

    Casting:
    - `CAST (column AS type)` or `column::type`
  30. @Kartones Kartones revised this gist Aug 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion postgres-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ Most `\d` commands support additional param of `__schema__.name__` and accept wi
    - `\q`: Quit/Exit
    - `\c __database__`: Connect to a database
    - `\d __table__`: Show table definition including triggers
    - `\dt`: List tables
    - `\dt *.*`: List tables from all schemas (if `*.*` is omitted will only show SEARCH_PATH ones)
    - `\l`: List databases
    - `\dn`: List schemas
    - `\df`: List functions