PostgresOSM.alter_table_schema

PostgresOSM.alter_table_schema(table_name, schema_name, new_schema_name, confirmation_required=True, verbose=False)

Move a table from one schema to another within the database being connected.

Parameters
  • table_name (str) – name of a table

  • schema_name (str) – name of a schema

  • new_schema_name – name of a new schema

  • confirmation_required (bool) – whether to prompt a message for confirmation to proceed, defaults to True

  • verbose (bool or int) – whether to print relevant information in console as the function runs, defaults to False

Examples:

>>> from pyhelpers.sql import PostgreSQL

>>> testdb = PostgreSQL('localhost', 5432, username='postgres', database_name='testdb')
Password (postgres@localhost:5432): ***
Connecting postgres:***@localhost:5432/testdb ... Successfully.

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

>>> # Create a new schema "test_schema"
>>> testdb.create_schema(schema_name='test_schema', verbose=True)
Creating a schema: "test_schema" ... Done.

>>> # Move the table "public"."test_table" to the schema "test_schema"
>>> testdb.alter_table_schema('test_table', 'public', 'test_schema', verbose=True)
To move the table "test_table" from the schema "public" to "test_schema"
? [No]|Yes: yes
Moving "public"."test_table" to "test_schema" ... Done.

>>> lst_tbl_names = testdb.list_table_names(schema_name='test_schema')
>>> print(lst_tbl_names)
['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.