dbTalk Databases Forums  

problem copying a table

comp.databases.postgresql comp.databases.postgresql


Discuss problem copying a table in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Manu
 
Posts: n/a

Default problem copying a table - 10-10-2006 , 03:09 AM






Hi all,
I want to copy a table with constraints, indexes, .... But with the "SELECT
* INTO MyTableCopy FROM MyTable" the column's properties aren't copied (as
the NOT NULL property).
Can someone help me please ?
Thank you

Manu



Reply With Quote
  #2  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: problem copying a table - 10-10-2006 , 04:15 AM






Manu <zzzzaaaaa (AT) blabla (DOT) com> wrote:
Quote:
I want to copy a table with constraints, indexes, .... But with the "SELECT
* INTO MyTableCopy FROM MyTable" the column's properties aren't copied (as
the NOT NULL property).
Can someone help me please ?
I would export the table with pg_dump.

Something like

pg_dump -f out.sql -s -n myschema -t mytable

Then edit out.sql and change names etc. appropriately.

Run the file as SQL script to create the new table.

There is also CREATE TABLE ... LIKE ...
but this will only copy NOT NULL constraints and column defaults.

Yours,
Laurenz Albe


Reply With Quote
  #3  
Old   
Manu
 
Posts: n/a

Default Re: problem copying a table - 10-10-2006 , 04:35 AM



Hi Laurenz,
I tried with CREATE TABLE ... LIKE ... but it doesn't run :

CREATE TABLE MyTable LIKE MyOldTable;
ERROR: parser: parse error at or near "like" at
character 23
Is the syntax correct ?
Thank you
Manu


"Laurenz Albe" <invite (AT) spam (DOT) to.invalid> a écrit dans le message de news:
1160471747.709058 (AT) proxy (DOT) dienste.wien.at...
Quote:
Manu <zzzzaaaaa (AT) blabla (DOT) com> wrote:
I want to copy a table with constraints, indexes, .... But with the
"SELECT
* INTO MyTableCopy FROM MyTable" the column's properties aren't copied
(as
the NOT NULL property).
Can someone help me please ?

I would export the table with pg_dump.

Something like

pg_dump -f out.sql -s -n myschema -t mytable

Then edit out.sql and change names etc. appropriately.

Run the file as SQL script to create the new table.

There is also CREATE TABLE ... LIKE ...
but this will only copy NOT NULL constraints and column defaults.

Yours,
Laurenz Albe



Reply With Quote
  #4  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: problem copying a table - 10-10-2006 , 06:38 AM



Manu <zzzzaaaaa (AT) blabla (DOT) com> wrote:
Quote:
I tried with CREATE TABLE ... LIKE ... but it doesn't run :

CREATE TABLE MyTable LIKE MyOldTable;
ERROR: parser: parse error at or near "like" at
character 23
Is the syntax correct ?
No, it should be

CREATE TABLE MyTable (LIKE MyOldTable);

If you want to copy column defaults, use the INCLUDING DEFAULTS clause.

From 8.2 on there will also be an INCLUDING CONSTRAINTS clause to copy
constraints.

Yours,
Laurenz Albe


Reply With Quote
  #5  
Old   
Manu
 
Posts: n/a

Default Re: problem copying a table - 10-10-2006 , 07:27 AM



CREATE TABLE MyTable (LIKE MyOldTable);
It doesn't work. I have the message :
ERROR: LIKE in table definitions not yet supported

I don't know how to do it....
Thank you Laurenz
Manu

"Laurenz Albe" <invite (AT) spam (DOT) to.invalid> a écrit dans le message de news:
1160480329.382113 (AT) proxy (DOT) dienste.wien.at...
Quote:
Manu <zzzzaaaaa (AT) blabla (DOT) com> wrote:
I tried with CREATE TABLE ... LIKE ... but it doesn't run :

CREATE TABLE MyTable LIKE MyOldTable;
ERROR: parser: parse error at or near "like" at
character 23
Is the syntax correct ?

No, it should be

CREATE TABLE MyTable (LIKE MyOldTable);

If you want to copy column defaults, use the INCLUDING DEFAULTS clause.

From 8.2 on there will also be an INCLUDING CONSTRAINTS clause to copy
constraints.

Yours,
Laurenz Albe



Reply With Quote
  #6  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: problem copying a table - 10-10-2006 , 09:40 AM



Manu <zzzzaaaaa (AT) blabla (DOT) com> wrote:
Quote:
CREATE TABLE MyTable (LIKE MyOldTable);
It doesn't work. I have the message :
ERROR: LIKE in table definitions not yet supported
Then your PostgreSQL version is probably too old.

You can either create the table by hand or use pg_dump as explained
in my first reply.

You can also try a client tool like pgAdminIII that can generate a
CREATE TABLE statement for a table.

Yours,
Laurenz Albe



Reply With Quote
  #7  
Old   
Manu
 
Posts: n/a

Default Re: problem copying a table - 10-10-2006 , 10:32 AM



I will try it
Thank you very much
Manu

"Laurenz Albe" <invite (AT) spam (DOT) to.invalid> a écrit dans le message de news:
1160491246.752727 (AT) proxy (DOT) dienste.wien.at...
Quote:
Manu <zzzzaaaaa (AT) blabla (DOT) com> wrote:
CREATE TABLE MyTable (LIKE MyOldTable);
It doesn't work. I have the message :
ERROR: LIKE in table definitions not yet supported

Then your PostgreSQL version is probably too old.

You can either create the table by hand or use pg_dump as explained
in my first reply.

You can also try a client tool like pgAdminIII that can generate a
CREATE TABLE statement for a table.

Yours,
Laurenz Albe




Reply With Quote
  #8  
Old   
HansH
 
Posts: n/a

Default Re: problem copying a table - 10-10-2006 , 02:36 PM



"Laurenz Albe" <invite (AT) spam (DOT) to.invalid> schreef in bericht
news:1160491246.752727 (AT) proxy (DOT) dienste.wien.at...
Quote:
Manu <zzzzaaaaa (AT) blabla (DOT) com> wrote:
CREATE TABLE MyTable (LIKE MyOldTable);
It doesn't work. I have the message :
ERROR: LIKE in table definitions not yet supported

Then your PostgreSQL version is probably too old.
Or it's a typo -( misplaced -: try
CREATE TABLE MyTable LIKE ( MyOldTable );

HansH




Reply With Quote
  #9  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: problem copying a table - 10-11-2006 , 02:09 AM



HansH <hans (AT) somewhere (DOT) invalid> wrote:
Quote:
Or it's a typo -( misplaced -: try
CREATE TABLE MyTable LIKE ( MyOldTable );
Your are guessing, right?
Is there any version of PostgreSQL that supports that syntax?

Yours,
Laurenz Albe


Reply With Quote
  #10  
Old   
Manu
 
Posts: n/a

Default Re: problem copying a table - 10-11-2006 , 02:58 AM



Hi HansH and Laurenz,
I think that my version not support this syntax. I'm using the 7.3.4
Thank you very much
Manu



"Laurenz Albe" <invite (AT) spam (DOT) to.invalid> a écrit dans le message de news:
1160550550.964123 (AT) proxy (DOT) dienste.wien.at...
Quote:
HansH <hans (AT) somewhere (DOT) invalid> wrote:
Or it's a typo -( misplaced -: try
CREATE TABLE MyTable LIKE ( MyOldTable );

Your are guessing, right?
Is there any version of PostgreSQL that supports that syntax?

Yours,
Laurenz Albe



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.