6

I have a dataframe with an index that I want to store in a postgresql database. For this I use df.to_sql(table_name,engine,if_exists='replace', index=True,chunksize=10000)

The index column from the pandas dataframe is copied to the database but is not set as primary key.

There are two solutions that require an additional step:

  1. specify a schema df.to_sql(schema=) docs
  2. Set the primary key after the table is ingested. query:

    ALTER TABLE table_name ADD PRIMARY KEY (id_column_name)

Is there a way to set the primary key without specifying the schema or altering the table?

1

1 Answer 1

2

After calling to_sql:

import sqlalchemy
engine = create_engine()
engine.execute('ALTER TABLE schema.table ADD PRIMARY KEY (keycolumn);')

Unfortunately, pandas.to_sql doesn't set primary key, it even also destructs the primary key of existing table. One must aware for the primary keys.

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

Comments

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.