2
igers = create_engine('postgres://%s:%s@%s/ibdrs' %(igersUser, igersPwd, igersHost), encoding="utf-8")
df.to_sql('Table_Named_Something', igers, if_exists='replace', schema='Schema', index = False)

Using the above code I am getting an error (psycopg2.ProgrammingError) Relation "Table_Named_Something" already exists

I thought that the if_exists='replace' should drop and recreate the table; am I wrong? why would that not work on redshift?

Shenanigator
  • 1,036
  • 12
  • 45
  • No I don't have a table named table. It has another name, but the data is sensitive and I have to sanitize it to make sure that there is 0 chance someone happens upon it. – Shenanigator Jan 31 '19 at 17:30
  • Once again, that is not the table name. Let me change the code so you can get unstuck there. I have already verified the permissions. – Shenanigator Jan 31 '19 at 17:38
  • I didn't realize I didn't copy the igers variable part sorry about that. Igers is the variable create_engine is assigned to. Beyond that you cannot have the rest since it is login and database address data – Shenanigator Jan 31 '19 at 17:49
  • Are you very sure you are using *replace* and NOT *append*? Check code carefully. Similar Redshift/Pandas posts show issue emerges with *append*: https://stackoverflow.com/q/54240688/1422451 (weeks ago); https://stackoverflow.com/questions/51312684/pandas-dataframe-to-redshift-psql-table (months ago). But no issue with *replace* (lots of upvotes): https://stackoverflow.com/a/39709820/1422451 (years ago). – Parfait Jan 31 '19 at 18:14
  • Yes I am very sure it says replace. Good to know about the append though. – Shenanigator Jan 31 '19 at 18:18
  • See last link as I just edited comment with no issue with *replace*. It must be your user privileges as you may not have ownership privileges. Try running an engine query: `DROP TABLE IF EXISTS my_schema.my_table` and check any error. And if no error run it before the `to_sql` call. – Parfait Jan 31 '19 at 18:20
  • the drop command works without issue. The user I am using is a superuser, so it shouldn't even need specific permissions; I did give it them though. – Shenanigator Jan 31 '19 at 18:24

1 Answers1

10

It will sound weird, but I faced the same issue and it was solved when I changed the table name to lowercase.

ROAMLAG
  • 126
  • 2
  • 6