如何编辑 PostgreSQL 存储过程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9860795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to edit PostgreSQL stored procedure?
提问by Paul
I'm slowly moving from MSSQL to PostgreSQL.
我正在慢慢地从 MSSQL 转向 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.
在 MSSQL 中,我可以调用已保存的过程或函数的编辑,并且管理外壳 (SQL Server Management Studio) 向我显示了过程的文本,因此我不必将其源代码存储在文本文件中的某处。
How to do the same with PostgreSQL the convenient way? I'm using pgAdmin III.
如何以方便的方式对 PostgreSQL 做同样的事情?我正在使用 pgAdmin III。
回答by Milen A. Radev
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.
Postgres 的官方发行版中包含 2 个客户端 - CLI 一个psql
和 GUI 一个pgAdmin
。两者都支持您想要的:对于psql
它\ef
和对于pgAdmin
- 右键单击功能,“属性”,“代码”选项卡。
回答by Erwin Brandstetter
In pgAdminyou can make your life easier if you activate this option:
在pgAdmin 中,如果您激活此选项,您可以使您的生活更轻松:
File -> Options.. -> Query Tool -> [x] Copy SQL from main form to SQL dialogue
File -> Options.. -> Query Tool -> [x] Copy SQL from main form to SQL dialog
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.
然后,SQL 窗格中显示的任何内容都将复制到新打开的查询工具窗口中。因此,在对象浏览器中选择该功能并单击工具栏中的放大镜图标。
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!
请注意当前版本 1.14.2 中的一个未解决的错误。默认情况下,public
具有EXECUTE
函数权限。您可以使用REVOKE
此权限 - 这仅对SECURITY DEFINER
函数有用。但是REVOKE
在来自 pgAdmin 的反向工程 DDL 语句中缺少这一点(NULL 与空 ACL 混淆)。如果您删除并重新创建这样的函数,请小心!
回答by sergzach
It's also a convenient way to edit the code and test it.
这也是编辑代码和测试它的一种便捷方式。
1) Extract the code of a required SQL function from pgAdmin.
1) 从 pgAdmin 中提取所需 SQL 函数的代码。
2) Place the code with the function into file.sql.
2) 将带有函数的代码放入file.sql中。
3) Create a shell/bat file in the same directory with file.sql:
3)在与file.sql相同的目录下创建一个shell/bat文件:
psql -U postgres dbname < file.sql
4) Place a shortcut for the shell/bat file into a fast panel.
4) 将 shell/bat 文件的快捷方式放入快速面板中。
5) Edit the file with your favourite text editor and push the shortcut to update the function.
5) 使用您喜欢的文本编辑器编辑文件并推送快捷方式以更新功能。
回答by Evgeny Nozdrev
right click on the function in object tree (on the left side) -> Scripts -> Script CREATE
右键单击对象树中的函数(左侧)-> Scripts -> Script CREATE
-or-
-或者-
Execute new SQL query -> copy code of "create or replace function ..." to it
执行新的 SQL 查询 -> 将“创建或替换函数...”的代码复制到它
Then edit the script and do not forgot to execute it
然后编辑脚本不要忘记执行
回答by iandouglas
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.
phpPgAdmin 将让您编辑您的存储过程并在界面内编辑它们。强烈建议您在关于将它们存储在外部以进行版本控制的问题下留下评论。