3

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

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

And... there are some like: Create table if doesn't exist?
Yes, you can use CREATE TABLE IF NOT EXISTS since version 9.1.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.