import pickle


def create_posgresurl(fname='SQLAuth.sql', sqldb_dict=None):
    """
    Creates a PostGresSQL url that can be used to connect to a Database. 
    
    Parameters
    ----------
    sqldb_dict: dict, optional
        If None the function will load the SQL credentials pickled file. Otherwise SQL credentials can be
        explicitely provided using this parameter.
    
    dir_path: Path object, optional
        Path to SQL credentials pickled file. SQL credentials can be created using create_postgres_authdict()
        or explicitely provided. Default is pulled from config files.
        Unnecessary if sqldb_dict is provided.
        Default sql_db
      
    filename: str, optional
        Filename of the SQL credentials pickled file.
        Defailt SQLAuth.sql
       
    Returns
    -------
    Str
        PostgreSQL url to connect to a Database

    Raises
    ------
    IOError: If credentials file cannot be loaded.
        
    """


    if not sqldb_dict:
        try:
            sqldb_dict = pickle.load(open(fname, 'rb'))
        except:
            raise IOError('ERROR: Could not load SQL credentials. Check path.')
    return 'postgres://{}:{}@{}/{}'.format(sqldb_dict['username'], sqldb_dict['password'], sqldb_dict['host:port'],
                                           sqldb_dict['dbname'])