{"id":1538,"date":"2017-09-13T08:59:51","date_gmt":"2017-09-13T12:59:51","guid":{"rendered":"http:\/\/shirishranjit.com\/blog1\/?page_id=1538"},"modified":"2017-09-13T08:59:51","modified_gmt":"2017-09-13T12:59:51","slug":"postgresql-commands","status":"publish","type":"page","link":"https:\/\/shirishranjit.com\/blog1\/technical-posts\/postgres-database-cheatsheet\/postgresql-commands","title":{"rendered":"Postgresql Commands"},"content":{"rendered":"<p>## PSQL<\/p>\n<p>Magic words:<br \/>\n&#8220;`bash<br \/>\npsql -U postgres<br \/>\n&#8220;`<br \/>\nIf run with `-E` flag, it will describe the underlaying queries of the `\\` commands (cool for learning!).<\/p>\n<p>Most `\\d` commands support additional param of `__schema__.name__` and accept wildcards like `*.*`<\/p>\n<p>&#8211; `\\q`: Quit\/Exit<br \/>\n&#8211; `\\c __database__`: Connect to a database<br \/>\n&#8211; `\\d __table__`: Show table definition including triggers<br \/>\n&#8211; `\\dt *.*`: List tables from all schemas (if `*.*` is omitted will only show SEARCH_PATH ones)<br \/>\n&#8211; `\\l`: List databases<br \/>\n&#8211; `\\dn`: List schemas<br \/>\n&#8211; `\\df`: List functions<br \/>\n&#8211; `\\dv`: List views<br \/>\n&#8211; `\\df+ __function__` : Show function SQL code.<br \/>\n&#8211; `\\x`: Pretty-format query results instead of the not-so-useful ASCII tables<\/p>\n<p>User Related:<br \/>\n&#8211; `\\du`: List users<br \/>\n&#8211; `\\du __username__`: List a username if present.<br \/>\n&#8211; `create role __test1__`: Create a role with an existing username.<br \/>\n&#8211; `create role __test2__ noinherit login password __passsword__;`: Create a role with username and password.<br \/>\n&#8211; `set role __test__;`: Change role for current session to `__test__`.<br \/>\n&#8211; `grant __test2__ to __test1__;`: Allow `__test1__` to set its role as `__test2__`.<\/p>\n<p>## Configuration<\/p>\n<p>&#8211; Service management commands:<br \/>\n&#8220;`<br \/>\nsudo service postgresql stop<br \/>\nsudo service postgresql start<br \/>\nsudo service postgresql restart<br \/>\n&#8220;`<\/p>\n<p>&#8211; Changing verbosity &#038; querying Postgres log:<br \/>\n  <br \/>1) First edit the config file, set a decent verbosity, save and restart postgres:<br \/>\n&#8220;`<br \/>\nsudo vim \/etc\/postgresql\/9.3\/main\/postgresql.conf<\/p>\n<p># Uncomment\/Change inside:<br \/>\nlog_min_messages = debug5<br \/>\nlog_min_error_statement = debug5<br \/>\nlog_min_duration_statement = -1<\/p>\n<p>sudo service postgresql restart<br \/>\n&#8220;`<br \/>\n  2) Now you will get tons of details of every statement, error, and even background tasks like VACUUMs<br \/>\n&#8220;`<br \/>\ntail -f \/var\/log\/postgresql\/postgresql-9.3-main.log<br \/>\n&#8220;`<br \/>\n  3) How to add user who executed a PG statement to log (editing `postgresql.conf`):<br \/>\n&#8220;`<br \/>\nlog_line_prefix = &#8216;%t %u %d %a &#8216;<br \/>\n&#8220;`<\/p>\n<p>## Handy queries<br \/>\n&#8211; `SELECT * FROM pg_proc WHERE proname=&#8217;__procedurename__&#8217;`: List procedure\/function<br \/>\n&#8211; `SELECT * FROM pg_views WHERE viewname=&#8217;__viewname__&#8217;;`: List view (including the definition)<br \/>\n&#8211; `SELECT pg_size_pretty(pg_total_relation_size(&#8216;__table_name__&#8217;));`: Show DB table space in use<br \/>\n&#8211; `SELECT pg_size_pretty(pg_database_size(&#8216;__database_name__&#8217;));`: Show DB space in use<br \/>\n&#8211; `show statement_timeout;`: Show current user&#8217;s statement timeout<br \/>\n&#8211; `SELECT * FROM pg_indexes WHERE tablename=&#8217;__table_name__&#8217; AND schemaname=&#8217;__schema_name__&#8217;;`: Show table indexes<br \/>\n&#8211; Get all indexes from all tables of a schema:<br \/>\n&#8220;`sql<br \/>\nSELECT<br \/>\n   t.relname AS table_name,<br \/>\n   i.relname AS index_name,<br \/>\n   a.attname AS column_name<br \/>\nFROM<br \/>\n   pg_class t,<br \/>\n   pg_class i,<br \/>\n   pg_index ix,<br \/>\n   pg_attribute a,<br \/>\n    pg_namespace n<br \/>\nWHERE<br \/>\n   t.oid = ix.indrelid<br \/>\n   AND i.oid = ix.indexrelid<br \/>\n   AND a.attrelid = t.oid<br \/>\n   AND a.attnum = ANY(ix.indkey)<br \/>\n   AND t.relnamespace = n.oid<br \/>\n    AND n.nspname = &#8216;kartones&#8217;<br \/>\nORDER BY<br \/>\n   t.relname,<br \/>\n   i.relname<br \/>\n&#8220;`<br \/>\n&#8211; Execution data:<br \/>\n  &#8211; Queries being executed at a certain DB:<br \/>\n&#8220;`sql<br \/>\nSELECT datname, application_name, pid, backend_start, query_start, state_change, state, query<br \/>\n  FROM pg_stat_activity<br \/>\n  WHERE datname=&#8217;__database_name__&#8217;;<br \/>\n&#8220;`<br \/>\n  &#8211; Get all queries from all dbs waiting for data (might be hung):<br \/>\n&#8220;`sql<br \/>\nSELECT * FROM pg_stat_activity WHERE waiting=&#8217;t&#8217;<br \/>\n&#8220;`<br \/>\n  &#8211; Currently running queries with process pid:<br \/>\n&#8220;`sql<br \/>\nSELECT pg_stat_get_backend_pid(s.backendid) AS procpid,<br \/>\n  pg_stat_get_backend_activity(s.backendid) AS current_query<br \/>\nFROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;<br \/>\n&#8220;`<\/p>\n<p>Casting:<br \/>\n&#8211; `CAST (column AS type)` or `column::type`<br \/>\n&#8211; `&#8217;__table_name__&#8217;::regclass::oid`: Get oid having a table name<\/p>\n<p>Query analysis:<br \/>\n&#8211; `EXPLAIN __query__`: see the query plan for the given query<br \/>\n&#8211; `EXPLAIN ANALYZE __query__`: see and execute the query plan for the given query<br \/>\n&#8211; `ANALYZE [__table__]`: collect statistics  <\/p>\n<p>## Tools<br \/>\n&#8211; [pg-top](http:\/\/ptop.projects.pgfoundry.org\/): `top` for PG. `sudo apt-get install ptop` + `pg_top`<br \/>\n&#8211; [Unix-like reverse search in psql](https:\/\/dba.stackexchange.com\/questions\/63453\/is-there-a-psql-equivalent-of-bashs-reverse-search-history):<br \/>\n&#8220;`bash<br \/>\n$ echo &#8220;bind &#8220;^R&#8221; em-inc-search-prev&#8221; > $HOME\/.editrc<br \/>\n$ source $HOME\/.editrc<br \/>\n&#8220;` <\/p>\n<div class=\"twttr_buttons\"><div class=\"twttr_twitter\">\n\t\t\t\t\t<a href=\"http:\/\/twitter.com\/share?text=Postgresql+Commands\" class=\"twitter-share-button\" data-via=\"\" data-hashtags=\"\"  data-size=\"default\" data-url=\"https:\/\/shirishranjit.com\/blog1\/technical-posts\/postgres-database-cheatsheet\/postgresql-commands\"  data-related=\"\" target=\"_blank\">Tweet<\/a>\n\t\t\t\t<\/div><div class=\"twttr_followme\">\n\t\t\t\t\t\t<a href=\"https:\/\/twitter.com\/shiranjit\" class=\"twitter-follow-button\" data-size=\"default\"  data-show-screen-name=\"false\"  target=\"_blank\">Follow me<\/a>\n\t\t\t\t\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>## PSQL Magic words: &#8220;`bash psql -U postgres &#8220;` 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 `*.*` &hellip; <a href=\"https:\/\/shirishranjit.com\/blog1\/technical-posts\/postgres-database-cheatsheet\/postgresql-commands\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"parent":489,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1538","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/1538"}],"collection":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/comments?post=1538"}],"version-history":[{"count":1,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/1538\/revisions"}],"predecessor-version":[{"id":1539,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/1538\/revisions\/1539"}],"up":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/489"}],"wp:attachment":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/media?parent=1538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}