Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 335 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PostgreSQL "DESCRIBE TABLE"

#1
How do you perform the equivalent of Oracle's `DESCRIBE TABLE` in PostgreSQL with psql command?
Reply

#2
If you want to obtain it from query instead of psql, you can query the catalog schema. Here's a complex query that does that:

<pre><code><!-- language: sql -->SELECT
f.attnum AS number,
f.attname AS name,
f.attnum,
f.attnotnull AS notnull,
pg_catalog.format_type(f.atttypid,f.atttypmod) AS type,
CASE
WHEN p.contype = 'p' THEN 't'
ELSE 'f'
END AS primarykey,
CASE
WHEN p.contype = 'u' THEN 't'
ELSE 'f'
END AS uniquekey,
CASE
WHEN p.contype = 'f' THEN g.relname
END AS foreignkey,
CASE
WHEN p.contype = 'f' THEN p.confkey
END AS foreignkey_fieldnum,
CASE
WHEN p.contype = 'f' THEN g.relname
END AS foreignkey,
CASE
WHEN p.contype = 'f' THEN p.conkey
END AS foreignkey_connnum,
CASE
WHEN f.atthasdef = 't' THEN d.adsrc
END AS default
FROM pg_attribute f
JOIN pg_class c ON c.oid = f.attrelid
JOIN pg_type t ON t.oid = f.atttypid
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
LEFT JOIN pg_class AS g ON p.confrelid = g.oid
WHERE c.relkind = 'r'::char
AND n.nspname = '%s' -- Replace with Schema name
AND c.relname = '%s' -- Replace with table name
AND f.attnum > 0 ORDER BY number
;
</code></pre>

It's pretty complex but it does show you the power and flexibility of the PostgreSQL system catalog and should get you on your way to pg_catalog mastery ;-). Be sure to change out the %s's in the query. The first is Schema and the second is the table name.

Reply

#3
The psql equivalent of `DESCRIBE TABLE` is `\d table`.

See the psql portion of the PostgreSQL manual for more details.

Reply

#4
You may do a `\d *search pattern *` **with asterisks** to find tables that match the search pattern you're interested in.
Reply

#5
Try this (in the `psql` command-line tool):

\d+ tablename

See [the manual][1] for more info.


[1]:

[To see links please register here]

Reply

#6
You can do that with a psql slash command:

\d myTable describe table

It also works for other objects:

\d myView describe view
\d myIndex describe index
\d mySequence describe sequence

Source: [faqs.org][1]

[1]:

[To see links please register here]

Reply

#7
In addition to the command line `\d+ <table_name>` you already found, you could also use the [information-schema][1] to look up the column data, using [info_schema.columns][2]

SELECT *
FROM info_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#8
You can also check using below query

Select * from schema_name.table_name limit 0;

Expmple : My table has 2 columns name and pwd. Giving screenshot below.

[![Adding image][1]][1]

*Using PG admin3


[1]:
Reply

#9
Use this command

\d table name

like

\d queuerecords

Table "public.queuerecords"
Column | Type | Modifiers
-----------+-----------------------------+-----------
id | uuid | not null
endtime | timestamp without time zone |
payload | text |
queueid | text |
starttime | timestamp without time zone |
status | text |

Reply

#10
The best way to describe a table such as a column, type, modifiers of columns, etc.

\d+ tablename or \d tablename

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through