Issues with connecting python/pandas, to a Postgress DB hosted on Django Digital Ocean

Viewed 38

I have difficulty finding my known tables in a Django/Postgress DB hosted on Digital Ocean. I seem only to get an outer layer of the database and not the tables working on the site's front end.

conn = psycopg2.connect(
    host=HOST,
    database=DATABASE,
    user=USERNAME,
    password=PASSWORD,
    port=PORT,
)

cur = conn.cursor()
cur.execute("""SELECT table_name FROM information_schema.tables""")
for table in cur.fetchall():
    print(table)

Here is a portion of the results, and not a single one is a known table books.

('pg_type',)
('pg_foreign_server',)
('pg_roles',)
('pg_settings',)
('pg_cursors',)
('pg_stat_bgwriter',)
('pg_subscription',)
('pg_stat_progress_vacuum',)
('pg_stat_progress_cluster',)
('pg_attribute',)
.
.
.
('view_table_usage',)
('foreign_server_options',)
('column_options',)
('foreign_data_wrapper_options',)
('foreign_tables',)
('foreign_data_wrappers',)
('foreign_servers',)
('foreign_table_options',)
('user_mappings',)
('user_mapping_options',)

I hope to finally run:

df = pd.read_sql('select * from books', my_eventual_successful_connection)

I get more discouraging results from SQLAlchemy, which does not output a result nor logging info for the following:

engine = create_engine(f"postgresql+psycopg2://doadmin:{PASSWORD}@{HOST}:{PORT}/{USERNAME}?sslmode=require")

from sqlalchemy import MetaData
metadata_obj = MetaData()
for t in metadata_obj.sorted_tables:
    print(t.name)

While this:

metadata_obj.tables.keys()

returns (including the logging):

INFO:sqlalchemy.engine.Engine:SELECT c.relname FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = %(schema)s AND c.relkind in ('r', 'p')
INFO:sqlalchemy.engine.Engine:[cached since 38.66s ago] {'schema': 'public'}

dict_keys([])

Though when I run:

with engine.connect() as connection:
    df = pd.read_sql('select * from user', connection)

df

I get

user
0 doadmin

note doadmin is the variable USERNAME

So finally, my results with querying the known table books I get the following:

ProgrammingError: (psycopg2.errors.UndefinedTable) relation "books" does not exist
LINE 1: select * from books

Any ideas?

Thanks

1 Answers

All this required was upgrading to a managed DB.

Related