def make_dsn_from_env(env):
    """
    Make DSN string from libpq environment variables.
    """
    ret = []
    mapping = {'PGHOST': 'host', 'PGPORT': 'port', 'PGUSER': 'user',
        'PGPASSWORD': 'password', 'PGDATABASE': 'dbname'}
    for env_name in ('PGHOST', 'PGPORT', 'PGUSER', 'PGPASSWORD', 'PGDATABASE'):
        value = env.get(env_name)
        if value:
            dsn_name = mapping[env_name]
            ret.append('{}={}'.format(dsn_name, value))
    return ' '.join(ret)
