__author__ = 'tuco'

from pymongo import MongoClient
import psycopg2
from psycopg2 import OperationalError, DatabaseError, IntegrityError
import json
from biomaj.config import BiomajConfig
import os
"""
This small script is a test script to transfert data from a Biomaj MongoDB
database into a PostgreSQL data using Jsonb data typexs
"""
if __name__ == '__main__':

    BiomajConfig.load_config()
    mongo_url = BiomajConfig.global_config.get('GENERAL', 'db.url')
    mongo_db = BiomajConfig.global_config.get('GENERAL', 'db.name')
    mc = MongoClient(mongo_url)
    m_bank = mc[mongo_db].banks
    banks = []

    insert_query = "INSERT INTO bank(data) VALUES "
    for bank in m_bank.find({}, {'_id': 0}):
        insert_query += "('%s')," % json.dumps(bank)
    insert_query = insert_query.strip(',')

    # In case we empty the databble

    try:
        pc = psycopg2.connect("dbname=biomaj user=%s password=%s port=%s" %
                              (os.getenv('PGUSER'), os.getenv('PGPASSWORD'),
                               os.getenv('PGPORT')))
        cursor = pc.cursor()
        cursor.execute(insert_query)
        pc.commit()
    except OperationalError as err:
        print("Can't connect to database:\n%s" % str(err.message))
    except DatabaseError as err:
        print("Error:\n%s" % str(err))








@@ -1,24 +1,203 @@
-__author__ = 'tuco'%0A
+#! /usr/bin/env python%0A%22%22%22%0AThis small script is a test script to transfert data from a Biomaj MongoDB%0Adatabase into a PostgreSQL data using Jsonb data typexs%0A%22%22%22%0Afrom __future__ import print_function
 %0Afro
@@ -375,146 +375,40 @@
  os%0A
-%22%22%22%0AThis small script is a test script to transfert data from a Biomaj MongoDB%0Adatabase into a PostgreSQL data using Jsonb data typexs%0A%22%22%22
+import sys%0A%0A__author__ = 'tuco'%0A
 %0Aif 
@@ -677,16 +677,38 @@
 nks = %5B%5D
+%0A    dbname = 'biomaj'
 %0A%0A    in
@@ -930,16 +930,71 @@
 databble
+%0A    if len(sys.argv) %3E 1:%0A        dbname = sys.argv%5B1%5D
 %0A%0A    tr
@@ -1034,22 +1034,18 @@
 %22dbname=
-biomaj
+%25s
  user=%25s
@@ -1103,27 +1103,14 @@
    (
-os.getenv('PGUSER')
+dbname
 , os
@@ -1124,16 +1124,12 @@
 ('PG
-PASSWORD
+USER
 '),%0A
@@ -1158,16 +1158,41 @@
         
+ os.getenv('PGPASSWORD'),
  os.gete

Support for pg dbname on command line