g2pg/g2pg.py
def df_to_db(df, table_name,schema=None, index_name='index'):
    """
    Writes a DataFrame to a the specified table in the PostgreSQL database.\n
    If the table exisits, it will update the rows and insert new rows, otherwise it will create the table.\n
    This uses environment variables to access the DB. Make sure your .env file contains the following (replace with the relevant data):\n
    DB_USER= 'username'
    DB_PW = 'super_secret_password'
    DB_URL = 'db_address'
    DB_NAME = 'my_exciting_db_name'
    Parameters
    ----------
    df : DataFrame
        The DataFrame to write to the db. Make sure your columns of of the dtype you want in the db.
    table_name : str
        The `table_name` to update or create the table with in the DB.
    schema : str, optional
        The schema where the table should be located. (default in None, which refers to the `public` schema)
    index_name : str, optional
        The index name (must be the index of your df). Default is `index`.

    """
    #'postgresql://username:password@db_address/db'
    try:
        engine = create_engine('postgresql://'+os.environ.get('DB_USER') +':'+os.environ.get('DB_PW')+'@'+os.environ.get('DB_URL')+'/'+ os.environ.get('DB_NAME'))
    except Exception as e:
        print(e)
        print('Could not establish connection to db. Please check credentials in .env file')
        sys.exit(1)
    try:
        df.index.name = index_name
        upsert(engine=engine,
            df=df,
            table_name=table_name,
            if_row_exists='update',
            schema=schema,
            dtype=None)
    except Exception as e:
        print(e)
        print('Could not write data to the specified table, check that the db credentials in .env file are correct and have write permissions')
        sys.exit(1)

    Writes a DataFrame to a the specified table in the PostgreSQL database.\n
    If the table exisits, it will update the rows and insert new rows, otherwise it will create the table.\n
    This uses environment variables to access the DB. Make sure your .env file contains the following (replace with the relevant data):\n
    DB_USER= 'username'
    DB_PW = 'super_secret_password'
    DB_URL = 'db_address'
    DB_NAME = 'my_exciting_db_name'
    Parameters
    ----------
    df : DataFrame
        The DataFrame to write to the db. Make sure your columns of of the dtype you want in the db.
    table_name : str
        The `table_name` to update or create the table with in the DB.
    schema : str, optional
        The schema where the table should be located. (default in None, which refers to the `public` schema)
    index_name : str, optional
        The index name (must be the index of your df). Default is `index`.

    
Writes a DataFrame to a the specified table in the PostgreSQL database.\n
If the table exisits, it will update the rows and insert new rows, otherwise it will create the table.\n
This uses environment variables to access the DB.

df : DataFrame
The DataFrame to write to the db. Make sure your columns of of the dtype you want in the db.
table_name : str
The `table_name` to update or create the table with in the DB.
schema : str, optional
The schema where the table should be located. (default in None, which refers to the `public` schema)
index_name : str, optional
The index name (must be the index of your df). Default is `index`.
Writes a DataFrame to a the specified table in the PostgreSQL database . \ n If the table exisits it will update the rows and insert new rows otherwise it will create the table . \ n This uses environment variables to access the DB . df : DataFrame The DataFrame to write to the db . Make sure your columns of of the dtype you want in the db . table_name : str The ` table_name ` to update or create the table with in the DB . schema : str optional The schema where the table should be located . ( default in None which refers to the ` public ` schema ) index_name : str optional The index name ( must be the index of your df ) . Default is ` index ` .
def df_to_db(df, table_name,schema=None, index_name='index'):
    try:
        engine = create_engine('postgresql://'+os.environ.get('DB_USER') +':'+os.environ.get('DB_PW')+'@'+os.environ.get('DB_URL')+'/'+ os.environ.get('DB_NAME'))
    except Exception as e:
        print(e)
        print('Could not establish connection to db. Please check credentials in .env file')
        sys.exit(1)
    try:
        df.index.name = index_name
        upsert(engine=engine,
            df=df,
            table_name=table_name,
            if_row_exists='update',
            schema=schema,
            dtype=None)
    except Exception as e:
        print(e)
        print('Could not write data to the specified table, check that the db credentials in .env file are correct and have write permissions')
        sys.exit(1)
def df_to_db ( df , table_name , schema = None , index_name = 'index' ) : try : engine = create_engine ( 'postgresql://' + os . environ . get ( 'DB_USER' ) + ':' + os . environ . get ( 'DB_PW' ) + '@' + os . environ . get ( 'DB_URL' ) + '/' + os . environ . get ( 'DB_NAME' ) ) except Exception as e : print ( e ) print ( 'Could not establish connection to db. Please check credentials in .env file' ) sys . exit ( 1 ) try : df . index . name = index_name upsert ( engine = engine , df = df , table_name = table_name , if_row_exists = 'update' , schema = schema , dtype = None ) except Exception as e : print ( e ) print ( 'Could not write data to the specified table, check that the db credentials in .env file are correct and have write permissions' ) sys . exit ( 1 )
Writes a DataFrame to a the specified table in the PostgreSQL database.\n
If the table exisits, it will update the rows and insert new rows, otherwise it will create the table.\n
This uses environment variables to access the DB.
Writes a DataFrame to a the specified table in the PostgreSQL database . \ n If the table exisits it will update the rows and insert new rows otherwise it will create the table . \ n This uses environment variables to access the DB .
"""
    Writes a DataFrame to a the specified table in the PostgreSQL database.\n
    If the table exisits, it will update the rows and insert new rows, otherwise it will create the table.\n
    This uses environment variables to access the DB. Make sure your .env file contains the following (replace with the relevant data):\n
    DB_USER= 'username'
    DB_PW = 'super_secret_password'
    DB_URL = 'db_address'
    DB_NAME = 'my_exciting_db_name'
    Parameters
    ----------
    df : DataFrame
        The DataFrame to write to the db. Make sure your columns of of the dtype you want in the db.
    table_name : str
        The `table_name` to update or create the table with in the DB.
    schema : str, optional
        The schema where the table should be located. (default in None, which refers to the `public` schema)
    index_name : str, optional
        The index name (must be the index of your df). Default is `index`.

    """ #'postgresql://username:password@db_address/db'
df
table_name
schema
index_name