Skip to content

Instantly share code, notes, and snippets.

@blackknight36
Last active February 26, 2017 15:55
Show Gist options
  • Select an option

  • Save blackknight36/bb0651a007d49bcb1f9b2f69aeca9874 to your computer and use it in GitHub Desktop.

Select an option

Save blackknight36/bb0651a007d49bcb1f9b2f69aeca9874 to your computer and use it in GitHub Desktop.
copy a mysql table to a postgresql table
#!/usr/bin/env python
# This script copies all records from a mysql database table into the
# corresponding table in the postgresql database. Tables must be created
# with the proper structure before running this script.
import psycopg2 as pgdb
import MySQLdb
host = 'localhost'
user = 'user'
pass = 'password'
database = 'database'
def connect_pg():
pgdb = pgdb.connect(host=server, user=user, password=password, database=database)
return pgdb
def connect_sq():
mysqldb = MySQLdb.connect (host = host, user = user, passwd=password, db=database)
return mysqldb
mysqldb = connect_sq()
c = mysqldb.cursor()
c.execute("SELECT * FROM apoc")
result = c.fetchall()
pgdb = connect_pg()
c2 = pgdb.cursor()
for record in result:
c2.execute('INSERT INTO apoc ("book", "chapter", "verse", "v_text") VALUES (%s, %s, %s, %s)', (record[1], record[2], record[3], record[4]))
db2.commit()
db2.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment