0

I'm trying to connect to one our our internal databases using the following code:

engine = create_engine('postgresql+psycopg2://{user}:{passw}@{host}:{port}/{db}'.format(
        user=config3.CANVAS_USERNAME,
        passw=config3.CANVAS_PWD,
        host=config3.CANVAS_BOUNCER,
        port=config3.CANVAS_PORT,
        db='cluster17dr'
    ))
metadata = MetaData()
    metadata.reflect(bind=engine)
    print(metadata.tables)

And my only result is a table called 'spatial_ref_sys', which I assume is some kind of metadata. I know that my login stuff is correct, because this works perfectly:

with ppg2.connect(
        database='cluster17dr',
        user=config3.CANVAS_USERNAME,
        password=config3.CANVAS_PWD,
        host=config3.CANVAS_BOUNCER,
        port=config3.CANVAS_PORT) as conn:
        cur = conn.cursor()
        sql = 'SELECT * FROM canvas.switchman_shards LIMIT 10'

        cur.execute(sql)

        res = cur.fetchall()
        print(res)

Any ideas as to what I'm missing in my connection using SQLAlchemy?

2
  • 1
    Try specifying metadata.reflect(..., schema="canvas")? Commented Apr 26, 2016 at 2:12
  • That did it. Create an answer and I'll accept:) Commented Apr 26, 2016 at 3:09

1 Answer 1

1

By default, if no schema name is specified, SQLAlchemy will only give you tables under the default schema. If you want to reflect tables in a schema other than the default schema (which defaults to public in PostgreSQL), you need to specify the schema keyword to .reflect():

metadata.reflect(..., schema="canvas")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.