I'm looking for a way to dump a PostgreSQL database schema using Python. Ideally, the result would be either a .sql dump or any other format that can later be used by SQLAlchemy to create a new database from that schema.
I made this experiment with SQLAlchemy's MetaData class:
from sqlalchemy import MetaData, create_engine
engine = create_engine(source_database_url)
test_engine = create_engine(test_database_url)
metadata = MetaData()
metadata.reflect(engine)
metadata.create_all(test_engine)
This does exactly what I want except for exporting the schema for later use. So, considering that SQLAlchemy can successfully reflect a schema and create another table based on it, I'm hoping there is a way to programmatically export it in the process.
I am aware that I could summon pg_dump and pg_restore from the code but I would like to avoid external dependencies and the troubles that come with them.
Is there a way to achieve this with SQLAlchemy or other Python libraries?