PostgreSQL.get_table_names

PostgreSQL.get_table_names(schema_name=None, verbose=False)[source]

Get the names of all tables in a schema.

Parameters:
  • schema_name (str | list | None) – name of a schema; when schema_name=None (default), it defaults to DEFAULT_SCHEMA (i.e. 'public')

  • verbose (bool | int) – whether to print relevant information in console, defaults to False

Returns:

table names of the given schema(s) schema_name

Return type:

dict

Examples:

>>> from pyhelpers.dbms import PostgreSQL

>>> testdb = PostgreSQL('localhost', 5432, 'postgres', database_name='testdb')
Password (postgres@localhost:5432): ***
Creating a database: "testdb" ... Done.
Connecting postgres:***@localhost:5432/testdb ... Successfully.

>>> tbl_names = testdb.get_table_names()
>>> tbl_names
{'public': []}

>>> tbl_names = testdb.get_table_names(schema_name='testdb', verbose=True)
The schema "testdb" does not exist.
>>> tbl_names
{'testdb': []}

>>> # Create a new table named "test_table" in the schema "testdb"
>>> new_tbl_name = 'test_table'
>>> col_spec = 'col_name_1 INT, col_name_2 TEXT'
>>> testdb.create_table(table_name=new_tbl_name, column_specs=col_spec, verbose=True)
Creating a table: "public"."test_table" ... Done.

>>> tbl_names = testdb.get_table_names(schema_name='public')
>>> tbl_names
{'public': ['test_table']}

>>> # Delete the database "testdb"
>>> testdb.drop_database(verbose=True)
To drop the database "testdb" from postgres:***@localhost:5432
? [No]|Yes: yes
Dropping "testdb" ... Done.