PostgreSQL.alter_table_schema

PostgreSQL.alter_table_schema(table_name, schema_name, new_schema_name, confirmation_required=True, verbose=False)[source]

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

Parameters:
  • table_name (str) – Name of the table.

  • schema_name (str) – Name of the current schema containing the table.

  • new_schema_name (str) – Name of the new schema to move the table to.

  • confirmation_required (bool) – Whether to prompt for confirmation before proceeding; defaults to True.

  • verbose (bool | int) – Whether to print relevant information to the console; defaults to False.

Examples:

>>> from pyhelpers.dbms import PostgreSQL
>>> testdb = PostgreSQL(database_name='testdb', verbose=True)
Password (postgres@localhost:5432): ***
Creating a database: "testdb" ... Done.
Connecting postgres:***@localhost:5432/testdb ... Successfully.
>>> # 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.
>>> # 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(
...     table_name='test_table', schema_name='public', new_schema_name='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.
>>> tbl_names = testdb.get_table_names(schema_name='test_schema')
>>> tbl_names
{'test_schema': ['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.