Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm running a batch of postgres queries from a python script. Some queries are as follow:

create table xxx [...]

Usually I get the following error:

psycopg2.ProgrammingError: relation "xxx" already exists

I know that i can manually delete the xxx table, but i ask me if there are a way to avoid this error. Something like delete xxx table if exist.

Thanks

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Yes, there's DROP TABLE IF EXISTS:

IF EXISTS

Do not throw an error if the table does not exist. A notice is issued in this case.

This option is available since version 8.2.

share|improve this answer
    
And... there are some like: Create table if doesn't exist? –  Kaervas Dec 14 '13 at 15:42
    
Yes, you can use CREATE TABLE IF NOT EXISTS since version 9.1. –  nwellnhof Dec 14 '13 at 17:27
    
Can you tell me what is wrong here? I can't run this query, version 9.1.9: create table if not exists fin_stat as select * from fin_dm –  Kaervas Dec 14 '13 at 20:29
    
StackOverflow is not a forum. Please ask a new question. –  nwellnhof Dec 15 '13 at 1:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.