Is there an equivalent of MySQL's SHOW CREATE TABLE
in Postgres? Is this possible? If not what is the next best solution?
I need the statement because I use it to create the table on an remote server (over WCF).
Is there an equivalent of MySQL's SHOW CREATE TABLE
in Postgres? Is this possible? If not what is the next best solution?
I need the statement because I use it to create the table on an remote server (over WCF).
pg_dump:
or use PostgreSQL GUI Tools(pgAdmin,phpPgAdmin,etc.)
You can try to trace in the PostgreSQL log file what
pg_dump --table table --schema-only
really does. Then you can use the same method to write your own sql function.In command line (
psql
) you can run:\d <table name>
to list all columns, their types and indexes.Building on the first part of @CubicalSoft's answer you can drop in the following function which should work for simple tables (assumes the default 'public' schema' and omits constraints, indexes and user defined data types etc. etc.). @RJS answer is the only way to do it properly at the moment; this is something that should be built into psql!
I realize I'm a bit late to this party, but this was the first result to my Google Search so I figured I'd answer with what I came up with.
You can get pretty far toward a solution with this query to get the columns:
And then this query for most common indexes:
Then it is a matter of building out the query string(s) in the right format.
Postgres extension ddlx (https://github.com/lacanoid/pgddl) does exactly this and more.
DBeaver is one of the best tools for SQL database management. You can get the table query like
create table table_name
in a very simple way in the DBeaver tool.In pgAdmin 4, just find the table in the tree on the left, e.g.:
When the table is selected, open the SQL tab on the right. It displays the
CREATE TABLE
for the selected table.As answered in https://serverfault.com/a/875414/333439, with the
\d <table>
meta-command inpsql
is possible to show the table structure in database. If you want to view the query used in meta-command, you can use the commandpsql -E
. As described in the manpage, the-E
switch echoes the\d
meta-commands queries. So, you can launchpsql -E
, you can view the table structure with\d <table>
meta-command, and, according to-E
switch, you can view the query generated for describe the table structureBetter