0

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?

d_b
  • 185
  • 1
  • 2
  • 13

1 Answers1

1

Try using double quotes:

abc = ('''SELECT * FROM "clubCategory" ''')

Also see this answer: https://stackoverflow.com/a/21798517/1453822

ky_aaaa
  • 290
  • 3
  • 10
  • Thanks! It worked! Instead of 'abc = ("""SELECT * FROM 'clubCategory' """)` , I did `abc = ('SELECT * FROM "clubCategory" ') and it worked. – d_b Oct 14 '20 at 13:31
  • This was the answer for your question? Please accept it and vote up. `https://stackoverflow.com/help/someone-answers` – ky_aaaa Oct 16 '20 at 14:22