Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get tables in postgres using psycopg2?

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

I am using postgresql-8.4 psycopg2.

like image 345
user1395784 Avatar asked May 15 '12 09:05

user1395784


People also ask

How do I get a list of tables in PostgreSQL?

To list the tables in the current database, you can run the \dt command, in psql : If you want to perform an SQL query instead, run this: SELECT table_name FROM information_schema.

How fetch data from PostgreSQL database in Python?

You can fetch data from PostgreSQL using the fetch() method provided by the psycopg2. The Cursor class provides three methods namely fetchall(), fetchmany() and, fetchone() where, The fetchall() method retrieves all the rows in the result set of a query and returns them as list of tuples.

How do you write a query in PostgreSQL Python?

To create a Postgres table in Python, we use the CREATE TABLE SQL statement. This query should be executed after establishing a connection to the database. We also create a cursor object by calling the cursor() method that belongs to the connection object. This cursor object is used to actually execute your commands.


1 Answers

This did the trick for me:

cursor.execute("""SELECT table_name FROM information_schema.tables        WHERE table_schema = 'public'""") for table in cursor.fetchall():     print(table) 
like image 53
kalu Avatar answered Oct 02 '22 20:10

kalu