Reputation: 195
I have a postgresql database "Test2" hosted in my localhost. I am able to see the tables using pgAdmin. I want to fetch the data of the DB from Jupyter Notebook. I tried to connect to the DB by following the steps shown in "2) of Part 2" of https://towardsdatascience.com/python-and-postgresql-how-to-access-a-postgresql-database-like-a-data-scientist-b5a9c5a0ea43
Thus, my code is --
import config as creds
import pandas as pd
def connect():
# Set up a connection to the postgres server.
conn_string = "host="+ creds.PGHOST +" port="+ "5432" +" dbname="+ creds.PGDATABASE +" user=" + creds.PGUSER \
+" password="+ creds.PGPASSWORD
conn = psycopg2.connect(conn_string)
print("Connected!")
# Create a cursor object
cursor = conn.cursor()
return conn, cursor
#Connecting to DB
conn, cursor = connect()
#SQL command to create inventory table
abc = ("""SELECT * FROM clubs""")
#Execute SQL Command and commit to DB
cursor.execute(abc)
results = cursor.fetchall()
print(results)
conn.commit()
My config.py looks like this -->
PGHOST = 'localhost'
PGDATABASE = 'Test2'
PGUSER = '#####'
PGPASSWORD = '#####'
I able to get the output when the table name has all lowercase characters but for table names which has mixed character like "clubCategory", it throws an error stating "relation "clubcategory" does not exist"
I tried
abc = ("""SELECT * FROM 'clubCategory' """)
but its still throws error.
Any help please?
Upvotes: 0
Views: 2687
Reputation: 320
Try using double quotes:
abc = ('''SELECT * FROM "clubCategory" ''')
Also see this answer: https://stackoverflow.com/a/21798517/1453822
Upvotes: 1