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

比如MySQL中的SHOW CREATE VIEW。


当前回答

select pg_get_viewdef('viewname', true)

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

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

其他回答

在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 definition from pg_views where viewname = 'my_view'

These is a little thing to point out. Using the function pg_get_viewdef or pg_views or information_schema.views you will always get a rewritten version of your original DDL. The rewritten version may or not be the same as your original DDL script. If the Rule Manager rewrite your view definition your original DLL will be lost and you will able to read the only the rewritten version of your view definition. Not all views are rewritten but if you use sub-select or joins probably your views will be rewritten.

select pg_get_viewdef('viewname', true)

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

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

找到'CREATE TABLE…的查询是使用这个查询-

SHOW TABLE your_schema_name.your_table_name