How do I get tables in postgres using psycopg2?

后端 未结 5 540
再見小時候
再見小時候 2021-01-31 01:48

Can someone please explain how I can get the tables in the current database?

I am using postgresql-8.4 psycopg2.

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 02:10

    pg_class stores all the required information.

    executing the below query will return user defined tables as a tuple in a list

    conn = psycopg2.connect(conn_string)
    cursor = conn.cursor()
    cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
    print cursor.fetchall()
    

    output:

    [('table1',), ('table2',), ('table3',)]
    

提交回复
热议问题