Last active
July 24, 2017 13:46
-
-
Save lorey/70b83e22fac07bb72ac423222f358614 to your computer and use it in GitHub Desktop.
PostgreSQL tables to csv (Backup database tables as CSV with Pandas)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pandas as pd | |
import psycopg2 | |
from dotenv import find_dotenv | |
from dotenv import load_dotenv | |
from psycopg2.extras import DictCursor | |
def main(): | |
conn = psycopg2.connect(os.environ.get("POSTGRES_DSN")) | |
conn.autocommit = True | |
tables = ['users', 'whatever'] | |
for table in tables: | |
# fetch | |
cursor = conn.cursor(cursor_factory=DictCursor) | |
cursor.execute('SELECT * FROM %s' % table) | |
rows = cursor.fetchall() | |
# convert to dict manually | |
dicts = [dict(row) for row in rows] | |
# save to csv | |
df = pd.DataFrame(dicts) | |
df.to_csv('data/raw/%s.csv' % table) | |
if __name__ == '__main__': | |
load_dotenv(find_dotenv()) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment