Kev
Kev

Reputation: 361

Psycopg2 relation db does not exist

I recently started using Macbook because my laptop was changed at work and right after that I started having problems with some of my code that I use to upload a dataframe to a postgresql database.

import psycopg2
from io import StringIO

def create_connection(user,password):
    return psycopg2.connect(
    host='HOST',
    database='DBNAME',
    user=user,
    password=password)

conn = create_connection(user,password)

table = "data_analytics.tbl_summary_wingmans_rt"
buffer = StringIO()
df.to_csv(buffer, header=False, index=False)
buffer.seek(0)
cursor = conn.cursor()
cursor.copy_from(buffer, table, sep=",", null="")
conn.commit()
cursor.close()

As you can see, the code is quite simple and even before the change of equipment it ran without major problem on Windows. But as soon as I run this same code on the mac it throws me the following error:

Error: relation "data_analytics.tbl_summary_wingmans_rt" does not exist

In several posts I saw that it could be the use of double quotes but I have already used the following and I still do not have a positive result.

"data_analytics."tbl_summary_wingmans_rt""
""data_analytics"."tbl_summary_wingmans_rt""
'data_analytics."tbl_summary_wingmans_rt"'

Upvotes: 1

Views: 2053

Answers (2)

Ture Pålsson
Ture Pålsson

Reputation: 6776

The behaviour of copy_from changed in psycopg2 2.9 to properly quote the table name, which means that you can no longer supply a schema-qualified table name that way; you have to use copy_expert instead.

Upvotes: 3

Nikhil B
Nikhil B

Reputation: 393

You have to separate schema and table before sending it to Postgres parser now, when you are sending "data_analytics.tbl_summary_wingmans_rt" its a single string and unable to parse

use '"data_analytics"."tbl_summary_wingmans_rt"' this will parse the output as "schema"."table" and PostgreSQL will be able to parse

Upvotes: 0

Related Questions