No table displayed to postgresql request using python

Viewed 59

I am trying to list tables from an already created database. In this database, there are 2 tables in the public schema, both have 1 field and no row yet.

From postgresql command line, I get these two tables displayed typing \d and also running this SQL command:

SELECT table_name
  FROM information_schema.tables
   WHERE table_schema='public'
   AND table_type='BASE TABLE';

I'd like to display the names of these tables using a python script: Here it is:

import pandas as pd
import psycopg2

# information used to create a database connection
sqluser = 'postgres'
dbname = 'testdb'

pwd = 'postgres'
# Connect to postgres database
con = psycopg2.connect(dbname=dbname, user=sqluser, password=pwd )
q = """
SELECT table_name
  FROM information_schema.tables
   WHERE table_schema='public'
   AND table_type='BASE TABLE';
"""
df = pd.read_sql_query(q, con)
df.head()
print("End of test")

I don't have any output from the sql request nor execution errors . Thanks for your help

1 Answers

you need to print it out :

print(df.head())

also in head(n) , default is n = 5 , so it you don't mention it it always return 5 rows.

Related