Specifying the schema in Pandas to_sql

The schema parameter in to_sql is confusing as the word “schema” means something different from the general meaning of “table definitions”. In some SQL flavors, notably postgresql, a schema is effectively a namespace for a set of tables.

For example, you might have two schemas, one called test and one called prod. Each might contain a table called user_rankings generated in pandas and written using the to_sql command. You would specify the test schema when working on improvements to user rankings. When you are ready to deploy the new rankings, you would write to the prod schema.

As others have mentioned, when you call to_sql the table definition is generated from the type information for each column in the dataframe. If the table already exists in the database with exactly the same structure, you can use the append option to add new data to the table.

Leave a Comment