简体   繁体   中英

How to edit PostgreSQL stored procedure?

I'm slowly moving from MSSQL to PostgreSQL.

In MSSQL I could call editing of already saved procedure or function, and the administration shell (SQL Server Management Studio) showed me procedure's text, so I did not have to store its source code somewhere in text file.

How to do the same with PostgreSQL the convenient way? I'm using pgAdmin III.

There're 2 clients included in the official distributions of Postgres - the CLI one psql and a GUI one pgAdmin . Both support what you want: for psql it's \\ef and for pgAdmin - right-click on function, "Properties", "Code" tab.

It's also a convenient way to edit the code and test it.

1) Extract the code of a required SQL function from pgAdmin.

2) Place the code with the function into file.sql.

3) Create a shell/bat file in the same directory with file.sql:

psql -U postgres dbname < file.sql

4) Place a shortcut for the shell/bat file into a fast panel.

5) Edit the file with your favourite text editor and push the shortcut to update the function.

In pgAdmin you can make your life easier if you activate this option:

File -> Options.. -> Query Tool -> [x] Copy SQL from main form to SQL dialogue

Then, whatever is displayed in the SQL pane will be copied to a newly opened Query Tool window. So, select the function in the object browser and click the magnifying glass icon in the tool bar.

Be aware of an open bug in the current version 1.14.2. By default, public has the EXECUTE privilege on functions. You can REVOKE this privilege - which is only useful for SECURITY DEFINER functions. But this REVOKE is missing in the reverse engineered DDL statements from pgAdmin (a NULL got confused with an empty ACL). Careful if you delete and recreate such a function!

right click on the function in object tree (on the left side) -> Scripts -> Script CREATE

-or-

Execute new SQL query -> copy code of "create or replace function ..." to it

Then edit the script and do not forgot to execute it

phpPgAdmin will let you edit your stored procedures and edit them within the interface. The comment left under your question about storing them externally for version control is highly recommended as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM