Dump PostgreSQL database schema with Python
<p>I'm looking for a way to dump a PostgreSQL database schema using Python. Ideally, the result would be either a <code>.sql</code> dump or any other format that can later be used by SQLAlchemy to create a new database from that schema.</p>

<p>I made this experiment with SQLAlchemy's <a href="http://docs.sqlalchemy.org/en/latest/core/metadata.html" rel="nofollow noreferrer">MetaData class</a>:</p>

<pre class="lang-python prettyprint-override"><code>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)
</code></pre>

<p>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.</p>

<p>I am aware that I could summon <code>pg_dump</code> and <code>pg_restore</code> from the code but I would like to avoid external dependencies and the troubles that come with them.</p>

<p>Is there a way to achieve this with SQLAlchemy or other Python libraries?</p>