PostgreSQL Rebuild View Script

Source: Internet
Author: User
Tags postgresql psql

Environment

Psql (9.3.6) Enter "Help" for information. root=# \d change_me table "Public.change_me" field | Type | Modifier words-----------+-----------------------+--------Too_short | Character varying (50) | root=# alter TABLE Change_me alter TOO_SHORT type VARCHAR (30); Error: Cannot use view or rule to change type description of a field: Rule _return in view v_change_me dependent on word section "Too_short"

Purpose: Change the Too_short type to VARCHAR (30) to derive the view from here V_change_me relies on this table.

First, the installation script:

Prerequisite: The Psql client is installed.

#curl firxiao.com/sh/recreate_views.sh-o recreate_views.sh|chmod +x recreate_views.sh

The procedure is as follows:

Second, the configuration script:

The edit script changes the following to your own and then saves the exit

# # #configHOST = "IP or hostname" db_name= "dbname" db_user= "USER" db_password= "PASSWORD" # # #

Third, view Help

#./recreate_views.sh Useage:recreate_views.sh:-B <view_name> #backup view and dependent objects Useage:recreate_ views.sh:-D <view_name> #delete View and Dependent objects Useage:recreate_views.sh:-C "<sql>" #exec Custom SQL command Useage:recreate_views.sh:-R <view_name> #restore view and dependent objects

Iv. whether the test can be connected properly

#./recreate_views.sh-c "\dt" List of relations Schema | Name | Type | Owner--------+-----------+-------+-------Public | Change_me | Table | Root (1 row)

V. Backup view

#./recreate_views.sh-b v_change_mebackup V_change_me to V_change_me_bak.sqlbackup V_change_me_too to V_change_me_ Bak.sql

The deleted views and dependencies will be automatically backed up to the view name _bak.sql

Vi. Deleting views

#./recreate_views.sh-d V_change_me Note: Recursively delete views V_change_me_toodrop view

Change table field type

#./recreate_views.sh-c "Alter TABLE Change_me alter Too_short TYPE VARCHAR (30);" ALTER TABLE

Vii. Restore deleted views

#./recreate_views.sh-r v_change_mesetsetsetsetsetsetset Error: View "V_change_me" does not exist setcreate Viewsetsetsetsetsetsetset error: View "V_change_me_too" does not exist setcreate view

Viii. Viewing table types

./recreate_views.sh-c "\d V_change_me" View "Public.v_change_me" Column | Type | Modifiers-----------+-----------------------+-----------Too_short | Character varying (30) |


Changes to table field types have been completed at this point


PostgreSQL Rebuild View Script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.