pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedGet the table creation DDL
24+ messages / 6 participants
[nested] [flat]
* Get the table creation DDL
@ 2022-07-10 15:40 Igor Korot <ikorot01@gmail.com>
0 siblings, 2 replies; 24+ messages in thread
From: Igor Korot @ 2022-07-10 15:40 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-general
Hi,
Is there a query I can execute that will give me CREATE TABLE() command
used to create a table?
Thank you.
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-10 16:12 Michael Nolan <htfoot@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
1 sibling, 1 reply; 24+ messages in thread
From: Michael Nolan @ 2022-07-10 16:12 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-general
On Sun, Jul 10, 2022 at 10:28 AM Igor Korot <ikorot01@gmail.com> wrote:
> Hi,
> Is there a query I can execute that will give me CREATE TABLE() command
> used to create a table?
>
> Thank you.
>
Use pg_dump --schema-only
--
Mike Nolan
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-10 16:47 Igor Korot <ikorot01@gmail.com>
parent: Michael Nolan <htfoot@gmail.com>
0 siblings, 3 replies; 24+ messages in thread
From: Igor Korot @ 2022-07-10 16:47 UTC (permalink / raw)
To: Michael Nolan <htfoot@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-general
Hi, Michael,
On Sun, Jul 10, 2022 at 11:13 AM Michael Nolan <htfoot@gmail.com> wrote:
>
>
>
> On Sun, Jul 10, 2022 at 10:28 AM Igor Korot <ikorot01@gmail.com> wrote:
>>
>> Hi,
>> Is there a query I can execute that will give me CREATE TABLE() command
>> used to create a table?
So there is no "query" per se?
Also how do I pass the table name?
Thank you.
>>
>> Thank you.
>
>
> Use pg_dump --schema-only
> --
> Mike Nolan
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-10 17:18 Igor Korot <ikorot01@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
2 siblings, 1 reply; 24+ messages in thread
From: Igor Korot @ 2022-07-10 17:18 UTC (permalink / raw)
To: Michael Nolan <htfoot@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-general
Hi,
On Sun, Jul 10, 2022 at 11:47 AM Igor Korot <ikorot01@gmail.com> wrote:
>
> Hi, Michael,
>
> On Sun, Jul 10, 2022 at 11:13 AM Michael Nolan <htfoot@gmail.com> wrote:
> >
> >
> >
> > On Sun, Jul 10, 2022 at 10:28 AM Igor Korot <ikorot01@gmail.com> wrote:
> >>
> >> Hi,
> >> Is there a query I can execute that will give me CREATE TABLE() command
> >> used to create a table?
>
> So there is no "query" per se?
> Also how do I pass the table name?
>
> Thank you.
>
> >>
> >> Thank you.
> >
> >
> > Use pg_dump --schema-only
In addition:
Can I send it to execute with PQexec() or SQLExecDirect()?
It is not a query, but an external command, so I'm wondering...
Thank you.
> > --
> > Mike Nolan
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-10 17:47 Ray O'Donnell <ray@rodonnell.ie>
parent: Igor Korot <ikorot01@gmail.com>
2 siblings, 1 reply; 24+ messages in thread
From: Ray O'Donnell @ 2022-07-10 17:47 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; Michael Nolan <htfoot@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-general
On 10/07/2022 17:47, Igor Korot wrote:
> Hi, Michael,
>
> On Sun, Jul 10, 2022 at 11:13 AM Michael Nolan <htfoot@gmail.com> wrote:
>>
>>
>>
>> On Sun, Jul 10, 2022 at 10:28 AM Igor Korot <ikorot01@gmail.com> wrote:
>>>
>>> Hi,
>>> Is there a query I can execute that will give me CREATE TABLE() command
>>> used to create a table?
>
> So there is no "query" per se?
> Also how do I pass the table name?
>
If you connect to the database with psql including the -E option, then do
\d <table name>
It will show you the SQL used to generate the output... this may help.
Ray.
--
Raymond O'Donnell // Galway // Ireland
ray@rodonnell.ie
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-10 17:57 Michael Nolan <htfoot@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
0 siblings, 1 reply; 24+ messages in thread
From: Michael Nolan @ 2022-07-10 17:57 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-general
I do not know those other tools, but there should be documentation for
them, as there is in the man page for how to process just one table using
pg_dump. You can pipe the output of pg_dump directly to psql, but I find
that's seldom useful.
--
Mike Nolan
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-10 18:50 Mladen Gogala <gogala.mladen@gmail.com>
parent: Michael Nolan <htfoot@gmail.com>
0 siblings, 0 replies; 24+ messages in thread
From: Mladen Gogala @ 2022-07-10 18:50 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 7/10/22 13:57, Michael Nolan wrote:
> I do not know those other tools, but there should be documentation for
> them, as there is in the man page for how to process just one table
> using pg_dump. You can pipe the output of pg_dump directly to psql,
> but I find that's seldom useful.
> --
> Mike Nolan
There is an extension which does precisely what you need:
https://github.com/MichaelDBA/pg_get_tabledef
In my opinion, that is a little inconsistency on the part of the
maintainers part because there are functions pg_get_functiondef,
pg_get_indexdef and pg_get_viewdef but nothing for tables.
https://www.postgresql.org/docs/14/functions-info.html
--
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-10 18:59 Igor Korot <ikorot01@gmail.com>
parent: Ray O'Donnell <ray@rodonnell.ie>
0 siblings, 0 replies; 24+ messages in thread
From: Igor Korot @ 2022-07-10 18:59 UTC (permalink / raw)
To: Ray O'Donnell <ray@rodonnell.ie>; +Cc: Michael Nolan <htfoot@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-general
Hi,
On Sun, Jul 10, 2022 at 12:47 PM Ray O'Donnell <ray@rodonnell.ie> wrote:
>
> On 10/07/2022 17:47, Igor Korot wrote:
> > Hi, Michael,
> >
> > On Sun, Jul 10, 2022 at 11:13 AM Michael Nolan <htfoot@gmail.com> wrote:
> >>
> >>
> >>
> >> On Sun, Jul 10, 2022 at 10:28 AM Igor Korot <ikorot01@gmail.com> wrote:
> >>>
> >>> Hi,
> >>> Is there a query I can execute that will give me CREATE TABLE() command
> >>> used to create a table?
> >
> > So there is no "query" per se?
> > Also how do I pass the table name?
> >
>
> If you connect to the database with psql including the -E option, then do
>
> \d <table name>
It means it is possible to have an actal query getting it...
Thank you.
>
> It will show you the SQL used to generate the output... this may help.
>
> Ray.
>
>
> --
> Raymond O'Donnell // Galway // Ireland
> ray@rodonnell.ie
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-10 19:26 Mladen Gogala <gogala.mladen@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
2 siblings, 1 reply; 24+ messages in thread
From: Mladen Gogala @ 2022-07-10 19:26 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 7/10/22 12:47, Igor Korot wrote:
> So there is no "query" per se?
> Also how do I pass the table name?
>
> Thank you.
You can create one from the catalog tables. Personally, I would use
INFORMATION_SCHEMA to avoid pg_class and pg_attribute. However, there is
an extension which does that for you. Somebody else has already done the
hard work.
--
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-10 21:00 Igor Korot <ikorot01@gmail.com>
parent: Mladen Gogala <gogala.mladen@gmail.com>
0 siblings, 1 reply; 24+ messages in thread
From: Igor Korot @ 2022-07-10 21:00 UTC (permalink / raw)
To: Mladen Gogala <gogala.mladen@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi,
On Sun, Jul 10, 2022 at 2:27 PM Mladen Gogala <gogala.mladen@gmail.com> wrote:
>
> On 7/10/22 12:47, Igor Korot wrote:
>
> So there is no "query" per se?
> Also how do I pass the table name?
>
> Thank you.
>
> You can create one from the catalog tables. Personally, I would use INFORMATION_SCHEMA to avoid pg_class and pg_attribute. However, there is an extension which does that for you. Somebody else has already done the hard work.
I understand.
The Problem is that I need to put this inside the C/ODBC interface for
my project.
I'm sure it is not a problem when people are working out of psql or
writing some scripts,
but for me it is painful to go and try to recreate it.
Now, I'm not sure if this extension can be freely re-used (query
extracted and placed
inside someone else's project).
Thank you.
>
> --
> Mladen Gogala
> Database Consultant
> Tel: (347) 321-1217
> https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 00:02 Mladen Gogala <gogala.mladen@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
0 siblings, 2 replies; 24+ messages in thread
From: Mladen Gogala @ 2022-07-11 00:02 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 7/10/22 17:00, Igor Korot wrote:
> I understand.
> The Problem is that I need to put this inside the C/ODBC interface for
> my project.
>
> I'm sure it is not a problem when people are working out of psql or
> writing some scripts,
> but for me it is painful to go and try to recreate it.
>
> Now, I'm not sure if this extension can be freely re-used (query
> extracted and placed
> inside someone else's project).
>
> Thank you.
Igor, https://github.com/MichaelDBA/pg_get_tabledef provides
"pg_get_tabledef" function which can be called from SQL and therefore
used from ODBC/C. This "extension" is nothing PL/PGSQL source code of
the function that returns DDL. That's about it. This is how it works:
mgogala@umajor Downloads]$ psql -h postgres -f
pg_get_tabledef-main/pg_get_tabledef.sql
Password for user mgogala:
DO
CREATE FUNCTION
[mgogala@umajor Downloads]$ psql -h postgres
Password for user mgogala:
psql (13.6, server 14.4)
WARNING: psql major version 13, server major version 14.
Some psql features might not work.
Type "help" for help.
mgogala=# select pg_get_tabledef('mgogala','emp');
pg_get_tabledef
---------------------------------------------------------------------
CREATE TABLE mgogala.emp ( +
empno smallint NOT NULL, +
ename character varying(10) NULL, +
job character varying(9) NULL, +
mgr smallint NULL, +
hiredate timestamp without time zone NULL, +
sal double precision NULL, +
comm double precision NULL, +
deptno smallint NULL, +
CONSTRAINT emp_pkey PRIMARY KEY (empno), +
CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept(deptno)+
) TABLESPACE pg_default; +
+
(1 row)
So, you clone the Git repository, run the "CREATE FUNCTION" script and,
voila, you can get the DDL for the desired table. Here is the same stuff
produced by the psql utility:
mgogala=# \d emp
Table "mgogala.emp"
Column | Type | Collation | Nullable | Default
----------+-----------------------------+-----------+----------+---------
empno | smallint | | not null |
ename | character varying(10) | | |
job | character varying(9) | | |
mgr | smallint | | |
hiredate | timestamp without time zone | | |
sal | double precision | | |
comm | double precision | | |
deptno | smallint | | |
Indexes:
"emp_pkey" PRIMARY KEY, btree (empno)
Foreign-key constraints:
"fk_deptno" FOREIGN KEY (deptno) REFERENCES dept(deptno)
And here is using the function from an ODBC connection:
|[mgogala@umajor Downloads]$ isql mgogala-pg
+---------------------------------------+ | Connected! | | | |
sql-statement | | help [tablename] | | quit | | |
+---------------------------------------+ SQL> select
pg_get_tabledef('mgogala','emp');
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| pg_get_tabledef |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CREATE TABLE mgogala.emp ( empno smallint NOT NULL, ename character
varying(10) NULL, job character varying(9) NULL, mgr smallint NULL,
hiredate timestamp without time zone NULL, sal double precision NULL,
comm double precision NULL, deptno smallint NULL, CONSTRAINT emp_pkey
PR...|
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
SQLRowCount returns 1 1 rows fetched SQL> |||
|The function description looks like this:|
|mgogala=# \df pg_get_tabledef
List of functions
Schema | Name | Result data type |
Argument data types
| Typ
e
--------+-----------------+------------------+----------------------------------
--------------------------------------------------------------------------------
---------------------------------------------------------------------------+----
--
public | pg_get_tabledef | text | in_schema character
varying, in_t
able character varying, in_fktype tabledef_fkeys DEFAULT
'FKEYS_INTERNAL'::table
def_fkeys, in_trigger tabledef_trigs DEFAULT
'NO_TRIGGERS'::tabledef_trigs | fun
c
(1 row)
|
|As expected, the function returns the "text" data type.
|
|Regards
|
||
||
--
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 00:07 Mladen Gogala <gogala.mladen@gmail.com>
parent: Mladen Gogala <gogala.mladen@gmail.com>
1 sibling, 0 replies; 24+ messages in thread
From: Mladen Gogala @ 2022-07-11 00:07 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 7/10/22 20:02, Mladen Gogala wrote:
> This "extension" is nothing PL/PGSQL source code
This "extension" is nothing _*but*_ PL/PGSQL source code
--
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 00:15 Igor Korot <ikorot01@gmail.com>
parent: Mladen Gogala <gogala.mladen@gmail.com>
1 sibling, 2 replies; 24+ messages in thread
From: Igor Korot @ 2022-07-11 00:15 UTC (permalink / raw)
To: Mladen Gogala <gogala.mladen@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi,
On Sun, Jul 10, 2022 at 7:02 PM Mladen Gogala <gogala.mladen@gmail.com> wrote:
>
> On 7/10/22 17:00, Igor Korot wrote:
>
> I understand.
> The Problem is that I need to put this inside the C/ODBC interface for
> my project.
>
> I'm sure it is not a problem when people are working out of psql or
> writing some scripts,
> but for me it is painful to go and try to recreate it.
>
> Now, I'm not sure if this extension can be freely re-used (query
> extracted and placed
> inside someone else's project).
>
> Thank you.
>
> Igor, https://github.com/MichaelDBA/pg_get_tabledef provides "pg_get_tabledef" function which can be called from SQL and therefore used from ODBC/C. This "extension" is nothing PL/PGSQL source code of the function that returns DDL. That's about it. This is how it works:
I understand.
The question here - does this "extension'' is a part of standard
PostgreSQL install?
And if not - can I copy and paste that code in my program?
Thank you.
>
> mgogala@umajor Downloads]$ psql -h postgres -f pg_get_tabledef-main/pg_get_tabledef.sql
> Password for user mgogala:
> DO
> CREATE FUNCTION
> [mgogala@umajor Downloads]$ psql -h postgres
> Password for user mgogala:
> psql (13.6, server 14.4)
> WARNING: psql major version 13, server major version 14.
> Some psql features might not work.
> Type "help" for help.
>
> mgogala=# select pg_get_tabledef('mgogala','emp');
> pg_get_tabledef
> ---------------------------------------------------------------------
> CREATE TABLE mgogala.emp ( +
> empno smallint NOT NULL, +
> ename character varying(10) NULL, +
> job character varying(9) NULL, +
> mgr smallint NULL, +
> hiredate timestamp without time zone NULL, +
> sal double precision NULL, +
> comm double precision NULL, +
> deptno smallint NULL, +
> CONSTRAINT emp_pkey PRIMARY KEY (empno), +
> CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept(deptno)+
> ) TABLESPACE pg_default; +
> +
>
> (1 row)
>
> So, you clone the Git repository, run the "CREATE FUNCTION" script and, voila, you can get the DDL for the desired table. Here is the same stuff produced by the psql utility:
>
> mgogala=# \d emp
> Table "mgogala.emp"
> Column | Type | Collation | Nullable | Default
> ----------+-----------------------------+-----------+----------+---------
> empno | smallint | | not null |
> ename | character varying(10) | | |
> job | character varying(9) | | |
> mgr | smallint | | |
> hiredate | timestamp without time zone | | |
> sal | double precision | | |
> comm | double precision | | |
> deptno | smallint | | |
> Indexes:
> "emp_pkey" PRIMARY KEY, btree (empno)
> Foreign-key constraints:
> "fk_deptno" FOREIGN KEY (deptno) REFERENCES dept(deptno)
>
> And here is using the function from an ODBC connection:
>
> [mgogala@umajor Downloads]$ isql mgogala-pg
> +---------------------------------------+
> | Connected! |
> | |
> | sql-statement |
> | help [tablename] |
> | quit |
> | |
> +---------------------------------------+
> SQL> select pg_get_tabledef('mgogala','emp');
> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> | pg_get_tabledef |
> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> | CREATE TABLE mgogala.emp (
> empno smallint NOT NULL,
> ename character varying(10) NULL,
> job character varying(9) NULL,
> mgr smallint NULL,
> hiredate timestamp without time zone NULL,
> sal double precision NULL,
> comm double precision NULL,
> deptno smallint NULL,
> CONSTRAINT emp_pkey PR...|
> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> SQLRowCount returns 1
> 1 rows fetched
> SQL>
>
>
> The function description looks like this:
>
> mgogala=# \df pg_get_tabledef
>
> List of functions
> Schema | Name | Result data type |
> Argument data types
> | Typ
> e
> --------+-----------------+------------------+----------------------------------
> --------------------------------------------------------------------------------
> ---------------------------------------------------------------------------+----
> --
> public | pg_get_tabledef | text | in_schema character varying, in_t
> able character varying, in_fktype tabledef_fkeys DEFAULT 'FKEYS_INTERNAL'::table
> def_fkeys, in_trigger tabledef_trigs DEFAULT 'NO_TRIGGERS'::tabledef_trigs | fun
> c
> (1 row)
>
> As expected, the function returns the "text" data type.
>
> Regards
>
> --
> Mladen Gogala
> Database Consultant
> Tel: (347) 321-1217
> https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 01:09 Rob Sargent <robjsargent@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
1 sibling, 1 reply; 24+ messages in thread
From: Rob Sargent @ 2022-07-11 01:09 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; +Cc: Mladen Gogala <gogala.mladen@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
> On Jul 10, 2022, at 6:16 PM, Igor Korot <ikorot01@gmail.com> wrote:
>
> Hi,
>
>> On Sun, Jul 10, 2022 at 7:02 PM Mladen Gogala <gogala.mladen@gmail.com> wrote:
>>
>> On 7/10/22 17:00, Igor Korot wrote:
>>
>> I understand.
>> The Problem is that I need to put this inside the C/ODBC interface for
>> my project.
>>
>> I'm sure it is not a problem when people are working out of psql or
>> writing some scripts,
>> but for me it is painful to go and try to recreate it.
>>
>> Now, I'm not sure if this extension can be freely re-used (query
>> extracted and placed
>> inside someone else's project).
>>
>> Thank you.
>>
>> Igor, https://github.com/MichaelDBA/pg_get_tabledef provides "pg_get_tabledef" function which can be called from SQL and therefore used from ODBC/C. This "extension" is nothing PL/PGSQL source code of the function that returns DDL. That's about it. This is how it works:
>
> I understand.
>
> The question here - does this "extension'' is a part of standard
> PostgreSQL install?
> And if not - can I copy and paste that code in my program?
>
> Thank you.
>
>>
>> mgogala@umajor Downloads]$ psql -h postgres -f pg_get_tabledef-main/pg_get_tabledef.sql
>> Password for user mgogala:
>> DO
>> CREATE FUNCTION
>> [mgogala@umajor Downloads]$ psql -h postgres
>> Password for user mgogala:
>> psql (13.6, server 14.4)
>> WARNING: psql major version 13, server major version 14.
>> Some psql features might not work.
>> Type "help" for help.
>>
>> mgogala=# select pg_get_tabledef('mgogala','emp');
>> pg_get_tabledef
>> ---------------------------------------------------------------------
>> CREATE TABLE mgogala.emp ( +
>> empno smallint NOT NULL, +
>> ename character varying(10) NULL, +
>> job character varying(9) NULL, +
>> mgr smallint NULL, +
>> hiredate timestamp without time zone NULL, +
>> sal double precision NULL, +
>> comm double precision NULL, +
>> deptno smallint NULL, +
>> CONSTRAINT emp_pkey PRIMARY KEY (empno), +
>> CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept(deptno)+
>> ) TABLESPACE pg_default; +
>> +
>>
>> (1 row)
>>
>> So, you clone the Git repository, run the "CREATE FUNCTION" script and, voila, you can get the DDL for the desired table. Here is the same stuff produced by the psql utility:
>>
>> mgogala=# \d emp
>> Table "mgogala.emp"
>> Column | Type | Collation | Nullable | Default
>> ----------+-----------------------------+-----------+----------+---------
>> empno | smallint | | not null |
>> ename | character varying(10) | | |
>> job | character varying(9) | | |
>> mgr | smallint | | |
>> hiredate | timestamp without time zone | | |
>> sal | double precision | | |
>> comm | double precision | | |
>> deptno | smallint | | |
>> Indexes:
>> "emp_pkey" PRIMARY KEY, btree (empno)
>> Foreign-key constraints:
>> "fk_deptno" FOREIGN KEY (deptno) REFERENCES dept(deptno)
>>
>> And here is using the function from an ODBC connection:
>>
>> [mgogala@umajor Downloads]$ isql mgogala-pg
>> +---------------------------------------+
>> | Connected! |
>> | |
>> | sql-statement |
>> | help [tablename] |
>> | quit |
>> | |
>> +---------------------------------------+
>> SQL> select pg_get_tabledef('mgogala','emp');
>> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
>> | pg_get_tabledef |
>> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
>> | CREATE TABLE mgogala.emp (
>> empno smallint NOT NULL,
>> ename character varying(10) NULL,
>> job character varying(9) NULL,
>> mgr smallint NULL,
>> hiredate timestamp without time zone NULL,
>> sal double precision NULL,
>> comm double precision NULL,
>> deptno smallint NULL,
>> CONSTRAINT emp_pkey PR...|
>> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
>> SQLRowCount returns 1
>> 1 rows fetched
>> SQL>
>>
>>
>> The function description looks like this:
>>
>> mgogala=# \df pg_get_tabledef
>>
>> List of functions
>> Schema | Name | Result data type |
>> Argument data types
>> | Typ
>> e
>> --------+-----------------+------------------+----------------------------------
>> --------------------------------------------------------------------------------
>> ---------------------------------------------------------------------------+----
>> --
>> public | pg_get_tabledef | text | in_schema character varying, in_t
>> able character varying, in_fktype tabledef_fkeys DEFAULT 'FKEYS_INTERNAL'::table
>> def_fkeys, in_trigger tabledef_trigs DEFAULT 'NO_TRIGGERS'::tabledef_trigs | fun
>> c
>> (1 row)
>>
>> As expected, the function returns the "text" data type.
>>
>> Regards
>>
>> --
>> Mladen Gogala
>> Database Consultant
>> Tel: (347) 321-1217
>> https://dbwhisperer.wordpress.com
>
>
Do it properly. Make it part of your coding infrastructure so it’s available in the next environment. You’ll thank yourself
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 01:12 Mladen Gogala <gogala.mladen@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
1 sibling, 0 replies; 24+ messages in thread
From: Mladen Gogala @ 2022-07-11 01:12 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 7/10/22 20:15, Igor Korot wrote:
> I understand.
>
> The question here - does this "extension'' is a part of standard
> PostgreSQL install?
> And if not - can I copy and paste that code in my program?
>
> Thank you.
Of course this function is not a part of the standard install. If it was
a part of the standard install, it wouldn't need a separate Git repo on
Gitlab. And of course you can use it in your database, just send me
$1000 and you will have no problems with the license. I believe there is
license file in the repo and it says something like that.
Regards
--
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 01:13 Igor Korot <ikorot01@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 2 replies; 24+ messages in thread
From: Igor Korot @ 2022-07-11 01:13 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: Mladen Gogala <gogala.mladen@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi,
On Sun, Jul 10, 2022 at 8:09 PM Rob Sargent <robjsargent@gmail.com> wrote:
>
>
>
> > On Jul 10, 2022, at 6:16 PM, Igor Korot <ikorot01@gmail.com> wrote:
> >
> > Hi,
> >
> >> On Sun, Jul 10, 2022 at 7:02 PM Mladen Gogala <gogala.mladen@gmail.com> wrote:
> >>
> >> On 7/10/22 17:00, Igor Korot wrote:
> >>
> >> I understand.
> >> The Problem is that I need to put this inside the C/ODBC interface for
> >> my project.
> >>
> >> I'm sure it is not a problem when people are working out of psql or
> >> writing some scripts,
> >> but for me it is painful to go and try to recreate it.
> >>
> >> Now, I'm not sure if this extension can be freely re-used (query
> >> extracted and placed
> >> inside someone else's project).
> >>
> >> Thank you.
> >>
> >> Igor, https://github.com/MichaelDBA/pg_get_tabledef provides "pg_get_tabledef" function which can be called from SQL and therefore used from ODBC/C. This "extension" is nothing PL/PGSQL source code of the function that returns DDL. That's about it. This is how it works:
> >
> > I understand.
> >
> > The question here - does this "extension'' is a part of standard
> > PostgreSQL install?
> > And if not - can I copy and paste that code in my program?
> >
> > Thank you.
> >
> >>
> >> mgogala@umajor Downloads]$ psql -h postgres -f pg_get_tabledef-main/pg_get_tabledef.sql
> >> Password for user mgogala:
> >> DO
> >> CREATE FUNCTION
> >> [mgogala@umajor Downloads]$ psql -h postgres
> >> Password for user mgogala:
> >> psql (13.6, server 14.4)
> >> WARNING: psql major version 13, server major version 14.
> >> Some psql features might not work.
> >> Type "help" for help.
> >>
> >> mgogala=# select pg_get_tabledef('mgogala','emp');
> >> pg_get_tabledef
> >> ---------------------------------------------------------------------
> >> CREATE TABLE mgogala.emp ( +
> >> empno smallint NOT NULL, +
> >> ename character varying(10) NULL, +
> >> job character varying(9) NULL, +
> >> mgr smallint NULL, +
> >> hiredate timestamp without time zone NULL, +
> >> sal double precision NULL, +
> >> comm double precision NULL, +
> >> deptno smallint NULL, +
> >> CONSTRAINT emp_pkey PRIMARY KEY (empno), +
> >> CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept(deptno)+
> >> ) TABLESPACE pg_default; +
> >> +
> >>
> >> (1 row)
> >>
> >> So, you clone the Git repository, run the "CREATE FUNCTION" script and, voila, you can get the DDL for the desired table. Here is the same stuff produced by the psql utility:
> >>
> >> mgogala=# \d emp
> >> Table "mgogala.emp"
> >> Column | Type | Collation | Nullable | Default
> >> ----------+-----------------------------+-----------+----------+---------
> >> empno | smallint | | not null |
> >> ename | character varying(10) | | |
> >> job | character varying(9) | | |
> >> mgr | smallint | | |
> >> hiredate | timestamp without time zone | | |
> >> sal | double precision | | |
> >> comm | double precision | | |
> >> deptno | smallint | | |
> >> Indexes:
> >> "emp_pkey" PRIMARY KEY, btree (empno)
> >> Foreign-key constraints:
> >> "fk_deptno" FOREIGN KEY (deptno) REFERENCES dept(deptno)
> >>
> >> And here is using the function from an ODBC connection:
> >>
> >> [mgogala@umajor Downloads]$ isql mgogala-pg
> >> +---------------------------------------+
> >> | Connected! |
> >> | |
> >> | sql-statement |
> >> | help [tablename] |
> >> | quit |
> >> | |
> >> +---------------------------------------+
> >> SQL> select pg_get_tabledef('mgogala','emp');
> >> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> >> | pg_get_tabledef |
> >> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> >> | CREATE TABLE mgogala.emp (
> >> empno smallint NOT NULL,
> >> ename character varying(10) NULL,
> >> job character varying(9) NULL,
> >> mgr smallint NULL,
> >> hiredate timestamp without time zone NULL,
> >> sal double precision NULL,
> >> comm double precision NULL,
> >> deptno smallint NULL,
> >> CONSTRAINT emp_pkey PR...|
> >> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> >> SQLRowCount returns 1
> >> 1 rows fetched
> >> SQL>
> >>
> >>
> >> The function description looks like this:
> >>
> >> mgogala=# \df pg_get_tabledef
> >>
> >> List of functions
> >> Schema | Name | Result data type |
> >> Argument data types
> >> | Typ
> >> e
> >> --------+-----------------+------------------+----------------------------------
> >> --------------------------------------------------------------------------------
> >> ---------------------------------------------------------------------------+----
> >> --
> >> public | pg_get_tabledef | text | in_schema character varying, in_t
> >> able character varying, in_fktype tabledef_fkeys DEFAULT 'FKEYS_INTERNAL'::table
> >> def_fkeys, in_trigger tabledef_trigs DEFAULT 'NO_TRIGGERS'::tabledef_trigs | fun
> >> c
> >> (1 row)
> >>
> >> As expected, the function returns the "text" data type.
> >>
> >> Regards
> >>
> >> --
> >> Mladen Gogala
> >> Database Consultant
> >> Tel: (347) 321-1217
> >> https://dbwhisperer.wordpress.com
> >
> >
>
> Do it properly. Make it part of your coding infrastructure so it’s available in the next environment. You’ll thank yourself
How should I do that?
Thank you.
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 01:14 Mladen Gogala <gogala.mladen@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
1 sibling, 1 reply; 24+ messages in thread
From: Mladen Gogala @ 2022-07-11 01:14 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 7/10/22 21:13, Igor Korot wrote:
> How should I do that?
>
> Thank you.
Oh boy! I give up.
--
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 01:34 Igor Korot <ikorot01@gmail.com>
parent: Mladen Gogala <gogala.mladen@gmail.com>
0 siblings, 0 replies; 24+ messages in thread
From: Igor Korot @ 2022-07-11 01:34 UTC (permalink / raw)
To: Mladen Gogala <gogala.mladen@gmail.com>; +Cc: Rob Sargent <robjsargent@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi,
On Sun, Jul 10, 2022 at 8:14 PM Mladen Gogala <gogala.mladen@gmail.com> wrote:
>
> On 7/10/22 21:13, Igor Korot wrote:
>
> How should I do that?
>
> Thank you.
>
> Oh boy! I give up.
Does he mean I need to make it as a GitHub module?
Can I even do that given that you use GitLab and my project is on GitHub?
Thank you.
>
> --
> Mladen Gogala
> Database Consultant
> Tel: (347) 321-1217
> https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 02:52 Rob Sargent <robjsargent@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
1 sibling, 1 reply; 24+ messages in thread
From: Rob Sargent @ 2022-07-11 02:52 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; +Cc: Mladen Gogala <gogala.mladen@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
>> Do it properly. Make it part of your coding infrastructure so it’s available in the next environment. You’ll thank yourself
> How should I do that?
>
> Thank you.
I don't know what build system you are using. How do get to a running
programme? Are you alone or on a team? The instruction above on getting
and using the piece you need are spot-on. It's how you tie that in to
your work configuration that is the next step.
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 08:11 Walter Dörwald <walter@livinglogic.de>
parent: Igor Korot <ikorot01@gmail.com>
1 sibling, 0 replies; 24+ messages in thread
From: Walter Dörwald @ 2022-07-11 08:11 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-general
On 10 Jul 2022, at 17:40, Igor Korot wrote:
> Hi,
> Is there a query I can execute that will give me CREATE TABLE()
> command
> used to create a table?
>
> Thank you.
I am using the following query for that:
```sql
select
a.attname,
a.attnum,
a.attnotnull,
pg_catalog.format_type(a.atttypid, a.atttypmod) as column_type,
case
when d.adrelid is not null then pg_catalog.pg_get_expr(d.adbin,
d.adrelid)
else null
end as default_value
from
pg_catalog.pg_class c
join
pg_catalog.pg_attribute a on c.oid = a.attrelid and a.attnum > 0 and
not a.attisdropped
join
pg_catalog.pg_namespace n on c.relnamespace = n.oid
left outer join
pg_catalog.pg_attrdef d on d.adrelid = a.attrelid and d.adnum =
a.attnum and a.atthasdef
where
c.relname = %s and
n.nspname = %s
order by
attnum
;
```
and the following Python code to format the `create table` statement:
```python
sql = f"create table {schema}.{name}\n"
sql += f"(\n"
for (last, column) in islast(cursor):
column_term = "" if last else ","
notnull = " not null" if column.attnotnull else ""
default = f" default {column.default_value}" if column.default_value is
not None else ""
sql += f"\t{column.attname}
{column.column_type}{default}{notnull}{column_term}\n"
sql += f");"
return sql
```
Servus,
Walter
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 12:31 Mladen Gogala <gogala.mladen@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 24+ messages in thread
From: Mladen Gogala @ 2022-07-11 12:31 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 7/10/22 22:52, Rob Sargent wrote:
> Are you alone or on a team?
What are your pronouns?
--
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-11 12:56 Rob Sargent <robjsargent@gmail.com>
parent: Mladen Gogala <gogala.mladen@gmail.com>
0 siblings, 1 reply; 24+ messages in thread
From: Rob Sargent @ 2022-07-11 12:56 UTC (permalink / raw)
To: Mladen Gogala <gogala.mladen@gmail.com>; Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 7/11/22 06:31, Mladen Gogala wrote:
> On 7/10/22 22:52, Rob Sargent wrote:
>> Are you alone or on a team?
>
> What are your pronouns?
>
This did make me chuckle, but no I am just asking whether or not the OP
is currently part of a team.
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-12 00:46 Igor Korot <ikorot01@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 24+ messages in thread
From: Igor Korot @ 2022-07-12 00:46 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: Mladen Gogala <gogala.mladen@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi,
On Mon, Jul 11, 2022 at 7:56 AM Rob Sargent <robjsargent@gmail.com> wrote:
>
> On 7/11/22 06:31, Mladen Gogala wrote:
> > On 7/10/22 22:52, Rob Sargent wrote:
> >> Are you alone or on a team?
No, this is my personal project.
Thank you.
> >
> > What are your pronouns?
> >
> This did make me chuckle, but no I am just asking whether or not the OP
> is currently part of a team.
>
^ permalink raw reply [nested|flat] 24+ messages in thread
* Re: Get the table creation DDL
@ 2022-07-12 03:08 Rob Sargent <robjsargent@gmail.com>
parent: Igor Korot <ikorot01@gmail.com>
0 siblings, 0 replies; 24+ messages in thread
From: Rob Sargent @ 2022-07-12 03:08 UTC (permalink / raw)
To: Igor Korot <ikorot01@gmail.com>; +Cc: Mladen Gogala <gogala.mladen@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
> On Jul 11, 2022, at 6:46 PM, Igor Korot <ikorot01@gmail.com> wrote:
>
> Hi,
>
>> On Mon, Jul 11, 2022 at 7:56 AM Rob Sargent <robjsargent@gmail.com> wrote:
>>
>>> On 7/11/22 06:31, Mladen Gogala wrote:
>>> On 7/10/22 22:52, Rob Sargent wrote:
>>>> Are you alone or on a team?
>
> No, this is my personal project.
>
> Thank you.
>
Do what ever works for you
>>>
>>> What are your pronouns?
>>>
>> This did make me chuckle, but no I am just asking whether or not the OP
>> is currently part of a team.
>>
^ permalink raw reply [nested|flat] 24+ messages in thread
end of thread, other threads:[~2022-07-12 03:08 UTC | newest]
Thread overview: 24+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-07-10 15:40 Get the table creation DDL Igor Korot <ikorot01@gmail.com>
2022-07-10 16:12 ` Michael Nolan <htfoot@gmail.com>
2022-07-10 16:47 ` Igor Korot <ikorot01@gmail.com>
2022-07-10 17:18 ` Igor Korot <ikorot01@gmail.com>
2022-07-10 17:57 ` Michael Nolan <htfoot@gmail.com>
2022-07-10 18:50 ` Mladen Gogala <gogala.mladen@gmail.com>
2022-07-10 17:47 ` Ray O'Donnell <ray@rodonnell.ie>
2022-07-10 18:59 ` Igor Korot <ikorot01@gmail.com>
2022-07-10 19:26 ` Mladen Gogala <gogala.mladen@gmail.com>
2022-07-10 21:00 ` Igor Korot <ikorot01@gmail.com>
2022-07-11 00:02 ` Mladen Gogala <gogala.mladen@gmail.com>
2022-07-11 00:07 ` Mladen Gogala <gogala.mladen@gmail.com>
2022-07-11 00:15 ` Igor Korot <ikorot01@gmail.com>
2022-07-11 01:09 ` Rob Sargent <robjsargent@gmail.com>
2022-07-11 01:13 ` Igor Korot <ikorot01@gmail.com>
2022-07-11 01:14 ` Mladen Gogala <gogala.mladen@gmail.com>
2022-07-11 01:34 ` Igor Korot <ikorot01@gmail.com>
2022-07-11 02:52 ` Rob Sargent <robjsargent@gmail.com>
2022-07-11 12:31 ` Mladen Gogala <gogala.mladen@gmail.com>
2022-07-11 12:56 ` Rob Sargent <robjsargent@gmail.com>
2022-07-12 00:46 ` Igor Korot <ikorot01@gmail.com>
2022-07-12 03:08 ` Rob Sargent <robjsargent@gmail.com>
2022-07-11 01:12 ` Mladen Gogala <gogala.mladen@gmail.com>
2022-07-11 08:11 ` Walter Dörwald <walter@livinglogic.de>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox