有没有一种简单的方法来查看使用PostgreSQL命令行客户端创建视图的代码?

比如MySQL中的SHOW CREATE VIEW。


当前回答

select pg_get_viewdef('viewname', true)

手册中列出了所有这些功能:

http://www.postgresql.org/docs/current/static/functions-info.html

其他回答

select definition from pg_views where viewname = 'my_view'

来自9.6及以上版本的好消息。视图编辑现在是原生的psql。只需调用\ev命令。视图定义将显示在您配置的编辑器中。

julian@assange=# \ev your_view_names

奖金。一些与查询缓冲区交互的有用命令。

Query Buffer
  \e [FILE] [LINE]       edit the query buffer (or file) with external editor
  \ef [FUNCNAME [LINE]]  edit function definition with external editor
  \ev [VIEWNAME [LINE]]  edit view definition with external editor
  \p                     show the contents of the query buffer
  \r                     reset (clear) the query buffer
  \s [FILE]              display history or save it to file
  \w FILE                write query buffer to file

在命令行客户端psql中,您可以使用以下命令:

\sv <VIEWNAME>

在psql cli中,可以使用

\d+ <yourViewName>
\sv <yourViewName>

输出如下:

\d+ v_ma_students

                               View "public.v_ma_students"
 Column |         Type          | Collation | Nullable | Default | Storage  | De
scription
--------+-----------------------+-----------+----------+---------+----------+---
SOMETHINGS HERE

View definition:
 SELECT student.sno,
    student.sname,
    student.ssex,
    student.sage,
    student.sdept
   FROM student
  WHERE student.sdept::text = 'MA'::text;
Options: check_option=cascaded


\sv v_ma_students

CREATE OR REPLACE VIEW public.v_ma_students AS
 SELECT student.sno,
    student.sname,
    student.ssex,
    student.sage,
    student.sdept
   FROM student
  WHERE student.sdept::text = 'MA'::text
 WITH CASCADED CHECK OPTION
select pg_get_viewdef('viewname', true)

手册中列出了所有这些功能:

http://www.postgresql.org/docs/current/static/functions-info.html