{"id":294,"date":"2015-05-14T16:27:35","date_gmt":"2015-05-14T16:27:35","guid":{"rendered":"https:\/\/www.tech.shinynewthings.com\/?p=294"},"modified":"2015-05-14T16:27:35","modified_gmt":"2015-05-14T16:27:35","slug":"postgres-sql-query-table-structure-and-metadata","status":"publish","type":"post","link":"https:\/\/www.tech.shinynewthings.com\/?p=294","title":{"rendered":"Postgres SQL &#8211; Query Table Structure and metadata"},"content":{"rendered":"<p>\/\/ USING POSTGRES SQL<\/p>\n<p>SELECT table_name<br \/>\n    FROM information_schema.tables<br \/>\nWHERE table_type = &#8216;BASE TABLE&#8217;<br \/>\n    AND table_schema NOT IN<br \/>\n        (&#8216;pg_catalog&#8217;, &#8216;information_schema&#8217;);<\/p>\n<p>SELECT column_name<br \/>\n    FROM information_schema.columns<br \/>\nWHERE table_name = &#8216;YourTablesName&#8217;;<\/p>\n<p>SELECT table_schema, table_name, column_name, data_type<br \/>\n    FROM information_schema.columns<br \/>\nWHERE table_schema = (&#8216;public&#8217;) and table_name in (&#8216;agencies&#8217;)<\/p>\n<p>SELECT table_schema, table_name, column_name, data_type, udt_name, character_maximum_length, udt_catalog<br \/>\n    FROM information_schema.columns<br \/>\nWHERE table_schema = (&#8216;public&#8217;) and table_name not like &#8216;pg_%&#8217; and table_name in (&#8216;agencies&#8217;) and udt_catalog = &#8216;ats_development&#8217;<\/p>\n<p>USING PSQL<\/p>\n<p>echo &#8216;\\d&#8217; | psql ats_development > tables.sql<\/p>\n<p>echo &#8216;\\d users&#8217; | psql ats_development > users.sql<\/p>\n<p>for table in $(&lt;listoftables); do<br \/>\n    echo &#8216;\\d&#8217; | psql<br \/>\ndone > $table.sql<\/p>\n<p>Information on quering information_schema:<\/p>\n<p>http:\/\/www.alberton.info\/postgresql_meta_info.html<\/p>\n<p>http:\/\/stackoverflow.com\/questions\/582657\/how-do-i-discover-the-structure-of-a-postgresql-database<\/p>\n<p>postgres=#   \\?<br \/>\nGeneral<br \/>\n  \\copyright             show PostgreSQL usage and distribution terms<br \/>\n  \\g [FILE] or ;         execute query (and send results to file or |pipe)<br \/>\n  \\h [NAME]              help on syntax of SQL commands, * for all commands<br \/>\n  \\q                     quit psql<\/p>\n<p>Query Buffer<br \/>\n  \\e [FILE] [LINE]       edit the query buffer (or file) with external editor<br \/>\n  \\ef [FUNCNAME [LINE]]  edit function definition with external editor<br \/>\n  \\p                     show the contents of the query buffer<br \/>\n  \\r                     reset (clear) the query buffer<br \/>\n  \\s [FILE]              display history or save it to file<br \/>\n  \\w FILE                write query buffer to file<\/p>\n<p>Input\/Output<br \/>\n  \\copy &#8230;              perform SQL COPY with data stream to the client host<br \/>\n  \\echo [STRING]         write string to standard output<br \/>\n  \\i FILE                execute commands from file<br \/>\n  \\o [FILE]              send all query results to file or |pipe<br \/>\n  \\qecho [STRING]        write string to query output stream (see \\o)<\/p>\n<p>Informational<br \/>\n  (options: S = show system objects, + = additional detail)<br \/>\n  \\d[S+]                 list tables, views, and sequences<br \/>\n  \\d[S+]  NAME           describe table, view, sequence, or index<br \/>\n  \\da[S]  [PATTERN]      list aggregates<br \/>\n  \\db[+]  [PATTERN]      list tablespaces<br \/>\n  \\dc[S]  [PATTERN]      list conversions<br \/>\n  \\dC     [PATTERN]      list casts<br \/>\n  \\dd[S]  [PATTERN]      show comments on objects<br \/>\n  \\ddp    [PATTERN]      list default privileges<br \/>\n  \\dD[S]  [PATTERN]      list domains<br \/>\n  \\det[+] [PATTERN]      list foreign tables<br \/>\n  \\des[+] [PATTERN]      list foreign servers<br \/>\n  \\deu[+] [PATTERN]      list user mappings<br \/>\n  \\dew[+] [PATTERN]      list foreign-data wrappers<br \/>\n  \\df[antw][S+] [PATRN]  list [only agg\/normal\/trigger\/window] functions<br \/>\n  \\dF[+]  [PATTERN]      list text search configurations<br \/>\n  \\dFd[+] [PATTERN]      list text search dictionaries<br \/>\n  \\dFp[+] [PATTERN]      list text search parsers<br \/>\n  \\dFt[+] [PATTERN]      list text search templates<br \/>\n  \\dg[+]  [PATTERN]      list roles<br \/>\n  \\di[S+] [PATTERN]      list indexes<br \/>\n  \\dl                    list large objects, same as \\lo_list<br \/>\n  \\dL[S+] [PATTERN]      list procedural languages<br \/>\n  \\dn[S+] [PATTERN]      list schemas<br \/>\n  \\do[S]  [PATTERN]      list operators<br \/>\n  \\dO[S+] [PATTERN]      list collations<br \/>\n  \\dp     [PATTERN]      list table, view, and sequence access privileges<br \/>\n  \\drds [PATRN1 [PATRN2]] list per-database role settings<br \/>\n  \\ds[S+] [PATTERN]      list sequences<br \/>\n  \\dt[S+] [PATTERN]      list tables<br \/>\n  \\dT[S+] [PATTERN]      list data types<br \/>\n  \\du[+]  [PATTERN]      list roles<br \/>\n  \\dv[S+] [PATTERN]      list views<br \/>\n  \\dE[S+] [PATTERN]      list foreign tables<br \/>\n  \\dx[+]  [PATTERN]      list extensions<br \/>\n  \\l[+]                  list all databases<br \/>\n  \\sf[+] FUNCNAME        show a function&#8217;s definition<br \/>\n  \\z      [PATTERN]      same as \\dp<\/p>\n<p>Formatting<br \/>\n  \\a                     toggle between unaligned and aligned output mode<br \/>\n  \\C [STRING]            set table title, or unset if none<br \/>\n  \\f [STRING]            show or set field separator for unaligned query output<br \/>\n  \\H                     toggle HTML output mode (currently off)<br \/>\n  \\pset NAME [VALUE]     set table output option<br \/>\n                         (NAME := {format|border|expanded|fieldsep|footer|null|<br \/>\n                         numericlocale|recordsep|tuples_only|title|tableattr|pager})<br \/>\n  \\t [on|off]            show only rows (currently off)<br \/>\n  \\T [STRING]            set HTML<\/p>\n<p>&lt;<\/p>\n<p>table> tag attributes, or unset if none<br \/>\n  \\x [on|off]            toggle expanded output (currently off)<\/p>\n<p>Connection<br \/>\n  \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]<br \/>\n                         connect to new database (currently &#8220;postgres&#8221;)<br \/>\n  \\encoding [ENCODING]   show or set client encoding<br \/>\n  \\password [USERNAME]   securely change the password for a user<br \/>\n  \\conninfo              display information about current connection<\/p>\n<p>Operating System<br \/>\n  \\cd [DIR]              change the current working directory<br \/>\n  \\timing [on|off]       toggle timing of commands (currently off)<br \/>\n  &#33; [COMMAND]           execute command in shell or start interactive shell<\/p>\n<p>Variables<br \/>\n  \\prompt [TEXT] NAME    prompt user to set internal variable<br \/>\n  \\set [NAME [VALUE]]    set internal variable, or list all if no parameters<br \/>\n  \\unset NAME            unset (delete) internal variable<\/p>\n<p>Large Objects<br \/>\n  \\lo_export LOBOID FILE<br \/>\n  \\lo_import FILE [COMMENT]<br \/>\n  \\lo_list<br \/>\n  \\lo_unlink LOBOID      large object operations<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ USING POSTGRES SQL SELECT table_name FROM information_schema.tables WHERE table_type = &#8216;BASE TABLE&#8217; AND table_schema NOT IN (&#8216;pg_catalog&#8217;, &#8216;information_schema&#8217;); SELECT column_name FROM information_schema.columns WHERE table_name = &#8216;YourTablesName&#8217;; SELECT table_schema, table_name, column_name, data_type FROM information_schema.columns WHERE table_schema = (&#8216;public&#8217;) and table_name in (&#8216;agencies&#8217;) SELECT table_schema, table_name, column_name, data_type, udt_name, character_maximum_length, udt_catalog FROM information_schema.columns WHERE table_schema = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294"}],"version-history":[{"count":1,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":295,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions\/295"}],"wp:attachment":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}