How to see the CREATE VIEW code for a view in PostgreSQL?

PostgresqlViewDefinition

Postgresql Problem Overview


Is there an easy way to see the code used to create a view using the PostgreSQL command-line client?

Something like the SHOW CREATE VIEW from MySQL.

Postgresql Solutions


Solution 1 - Postgresql

Kept having to return here to look up pg_get_viewdef (how to remember that!!), so searched for a more memorable command... and got it:

\d+ viewname

You can see similar sorts of commands by typing \? at the pgsql command line.

Bonus tip: The emacs command sql-postgres makes pgsql a lot more pleasant (edit, copy, paste, command history).

Solution 2 - Postgresql

select pg_get_viewdef('viewname', true)

A list of all those functions is available in the manual:

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

Solution 3 - Postgresql

select definition from pg_views where viewname = 'my_view'

Solution 4 - Postgresql

If you want an ANSI SQL-92 version:

select view_definition from information_schema.views where table_name = 'view_name';

Solution 5 - Postgresql

Good news from v9.6 and above. View editing are now native from psql. Just invoke \ev command. View definitions will show in your configured editor.

julian@assange=# \ev your_view_names

Bonus. Some useful command to interact with query buffer.

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

Solution 6 - Postgresql

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.

Solution 7 - Postgresql

In psql cli , you can use

\d+ <yourViewName>
\sv <yourViewName>

Output as follows:

\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

Solution 8 - Postgresql

In the command line client psql you can use following command:

\sv <VIEWNAME>

Solution 9 - Postgresql

The straightforward way to find the 'CREATE TABLE ...' query is to use this query -

SHOW TABLE your_schema_name.your_table_name

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElias DornelesView Question on Stackoverflow
Solution 1 - PostgresqlEoghanMView Answer on Stackoverflow
Solution 2 - Postgresqla_horse_with_no_nameView Answer on Stackoverflow
Solution 3 - PostgresqlClodoaldo NetoView Answer on Stackoverflow
Solution 4 - PostgresqlSteve JuddView Answer on Stackoverflow
Solution 5 - PostgresqlBrain90View Answer on Stackoverflow
Solution 6 - PostgresqlGianluca RossiniView Answer on Stackoverflow
Solution 7 - PostgresqlDeltaView Answer on Stackoverflow
Solution 8 - PostgresqltrunikovView Answer on Stackoverflow
Solution 9 - PostgresqlPrashant K TiwariView Answer on Stackoverflow