0

I am attempting to insert data into a PostgreSQL database using PySpark with JDBC. However, during the data insertion process, it is unexpectedly attempting to recreate the table and producing the following output.

org.postgresql.util.PSQLException: ERROR: relation "account" already exists

I am trying to use the below code snippet to write the data in Postgres.

def postgres_writes(url, driver, username, password, table_name, df):
df.write \
  .format("jdbc") \
  .option("url", url) \
  .option("dbtable", table_name) \
  .option("user", username) \
  .option("password", password) \
  .option("driver", driver) \
  .mode("append") \
  .save()

I want to append the data in an existing table.

1 Answer 1

1

Please make sure that, you are passing appropriate jdbc url. As per Postgres documentation Documentation Link you can use below JDBC URL's forms

jdbc:postgresql:database
jdbc:postgresql:/
jdbc:postgresql://host/database
jdbc:postgresql://host/
jdbc:postgresql://host:port/database
jdbc:postgresql://host:port/

Also try to recreate the table and try to load the data again. There might be some issue at Spark session level.

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

4 Comments

I am using jdbc:postgresql://host:port/database format
Ok. I tried your scenario. I am able to append the data.
Is there any configuration I need to change in the DB side?
Deleting and recreating the table resolve the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.