Kaervas
Kaervas

Reputation: 113

Postgresql: create a table and delete if exist

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

Upvotes: 3

Views: 4798

Answers (1)

nwellnhof
nwellnhof

Reputation: 33638

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.

Upvotes: 5

Related Questions