If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Create table if doesnt exists Postgre

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-08-11, 11:07
renatois renatois is offline
Registered User
 
Join Date: Sep 2011
Posts: 1
Exclamation Create table if doesnt exists Postgre

Hi, using Postgre DB, I need to create a Postgre DDL to check if a table exists and, if it exists, check the table fields and create the fields that doenst already exists.

Any help?

Thanks
Renato
Reply With Quote
  #2 (permalink)  
Old 09-09-11, 11:23
marc_ marc_ is offline
Registered User
 
Join Date: Aug 2011
Location: Glasgow, UK
Posts: 36
For the DDL part...

From your command line:
pg_dump -s -f mydb_ddl.sql mydb

where "mydb" is the name of your database.

Marc

Last edited by marc_; 09-09-11 at 11:49. Reason: added switch -s
Reply With Quote
  #3 (permalink)  
Old 10-10-11, 13:33
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
Rather than dump the entire database to a script file, I would instead use the Information Schema.

For your example, to determine the table/field existence:

Code:
Select table_name, column_name
From information_schema.columns
Where Table_Name = 'your_tablename' and schema_name = 'your_schemaname'
The records returns using the above query are the table name and column name(s) which are defined in the database.

If no records are returned, then there are no columns defined. (which would imply that the table doesn't exist... although it is possible, I suppose, that someone created a table, then deleted all the columns from that table.)

To verify table existence, whether columns exist or not, use
Code:
Select table_name, table_type
From information_schema.tables
Where Table_Name = 'your_tablename' and schema_name = 'your_schemaname'
If a record is returned, your table exists.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #4 (permalink)  
Old 10-11-11, 09:16
arvindps arvindps is offline
Registered User
 
Join Date: Aug 2011
Posts: 27
use

Quote:
select * from pg_tables where tablename = "mytable"
to verify if the table is present.
Reply With Quote
  #5 (permalink)  
Old 11-02-11, 05:45
shangwu shangwu is offline
Registered User
 
Join Date: Nov 2011
Posts: 1
hello

Yangtze River Cruise
i like the forum
Reply With Quote
Reply

Tags
postgre create table

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On