Skip to content

Instantly share code, notes, and snippets.

@mylamour
Created January 1, 2017 04:43
Show Gist options
  • Save mylamour/9b7b979451f9818f2d0c0f595cd07882 to your computer and use it in GitHub Desktop.
Save mylamour/9b7b979451f9818f2d0c0f595cd07882 to your computer and use it in GitHub Desktop.
use luigi to Backup Postgresql, there has a problem, it's not flexible. but i still think that's workflow should be
import luigi
import psycopg2
class QueryBackToTmp(luigi.Task):
def run(self):
conn_string = "host='ec2-54-zzz-xxx-yyy.cn-north-1.compute.amazonaws.com.cn' " \
"dbname='musicbrainz' " \
"user='postgres' " \
"password='password'"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
SQL = """ COPY (select artist.id as artist_id,
artist_credit.id as artist_credit_id ,
recording.id as recording_id ,
artist.gid as artist_gid,
recording.gid as recording_gid,
artist.name as artist_name,
artist_credit_name.artist as artist_credit_name_artist ,
artist_credit_name.name as artist_credit_name_name
from musicbrainz.artist,musicbrainz.artist_credit_name,musicbrainz.artist_credit,musicbrainz.recording
where artist.gid='a223958d-5c56-4b2c-a30a-87e357bc121b' limit 100)
TO '/home/tmp/tmp.csv' with csv;
"""
cursor.execute(SQL)
if __name__ == '__main__':
luigi.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment