Show the CREATE VIEW code for a view in PostgreSQL?
Asked Answered
G

9

266

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.

Goof answered 31/1, 2013 at 20:13 Comment(0)
D
329

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).

Delaminate answered 14/3, 2013 at 22:2 Comment(5)
I usually combine this trick with \o command. I dump \d+ to some files then using vim macro i modified those files to supplied my need.Soriano
Sad thing is: This cannot be used without psql. The "pure" SQL-command-version (without resorting pg_get_viewdef at all) can, which is more portable, e.g. to Perl with DBI.Clicker
What would be more useful is to be able to edit the view definition code directly with some variation of the \e command, like \ef for functions. A \ev feature would be nice. So far the solution suggested by @Soriano is the closest I've found to quick editing view definitions.Widescreen
Related tip: \dv lists all viewsFonzie
@Jim U edited the answer to \s+ instead of \d+ however that isn't valid in my version of postgresql ... he also said "l to list views, s to show code" ... however \l lists databases for me ... does anyone know whether any of the above is valid in newer postgresql?Delaminate
N
228
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

Nicotiana answered 31/1, 2013 at 20:36 Comment(4)
cool, it even pretty-prints it! :) the manual says it is deprecated, though... :( thanks!Goof
@elias: just use the version that uses an OID by casting the name to an oid: select pg_get_viewdef('viewname'::regclass, true)Nicotiana
@elias as an alternative to casting, this works too: SELECT pg_get_viewdef(to_regclass('viewname')) (requires at least v9.4).Beheld
A big thank you! I have spent at least last 30 minutes trying to find some quick answer, expecting that would be something like "sp_help viewname" like in T-SQL, but not that straightforward.Orthicon
L
83
select definition from pg_views where viewname = 'my_view'
Lisabethlisan answered 31/1, 2013 at 20:29 Comment(6)
thanks for this one.. it allows to access the view definition from my program rather than just from the psql-client.Palstave
This has the added benefit in that it works for Amazon Redshift as well.Discountenance
This does not work for views in schemas that are not on the search path. And it does not distinguish between two views with the same name in different schemas. When I write schema, I am referring to the namespace that you create with CREATE SCHEMAOuting
@MichaelDillon make a select * instead of select definition, and you will be able to see what schema the view is from, including some other informations.Fatherland
If your view is not on the search path, use select definition from pg_views where schemaname = 'my_schema' and viewname = 'my_view'Eoin
This is much better than using the function pg_get_viewdef().Defilade
P
26

If you want an ANSI SQL-92 version:

select view_definition from information_schema.views where table_name = 'view_name';
Protoxylem answered 9/8, 2016 at 15:48 Comment(1)
The great thing about information_schema is that unlike pg_catalog it's won't change structure between different postgresql versions.Merrygoround
S
15

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
Soriano answered 8/4, 2017 at 4:47 Comment(1)
Haha, Just to salute him. he commit a lot to psql in early days. git.postgresql.org/gitweb/…Soriano
P
12

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
Pappose answered 31/10, 2021 at 13:12 Comment(0)
I
11

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.

Immunity answered 27/9, 2018 at 11:13 Comment(1)
Important detail. This is annoying because I wanted the original "AS SELECT * FROM ..." statement.Centerboard
K
9

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

\dv <VIEWNAME>
Koppel answered 21/9, 2021 at 12:47 Comment(0)
A
0

For example, you create person table and my_v view as shown below:

CREATE TABLE person (
  id INTEGER,
  name VARCHAR(20)
);

CREATE VIEW my_v AS
  SELECT * FROM person;

Then, you can show the code of my_v view with \sv as shown below:

postgres=# \sv public.my_v
CREATE OR REPLACE VIEW public.my_v AS
 SELECT id,
    name
   FROM person

postgres=# \sv+ public.my_v
1          CREATE OR REPLACE VIEW public.my_v AS
2           SELECT id,
3              name
4             FROM person

*Memos:

  • + can show line numbers.

  • You can omit the schema public..

Or, you can show the code of my_v view with pg_get_viewdef() as shown below:

postgres=# SELECT pg_get_viewdef('public.my_v');
 pg_get_viewdef
-----------------
  SELECT id,    +
     name       +
    FROM person;
(1 row)

postgres=# SELECT pg_get_viewdef('public.my_v'::regclass);
 pg_get_viewdef
-----------------
  SELECT id,    +
     name       +
    FROM person;
(1 row)

*Memos:

  • You can use a view name or OID(Object identifier)forpg_get_viewdef()`.

  • You can omit the schema public..

Or, you can show the code of my_v view with \d+ <view-name> as shown below:

postgres=# \d+ public.my_v
                                    View "public.my_v"
 Column |         Type          | Collation | Nullable | Default | Storage  | Description
--------+-----------------------+-----------+----------+---------+----------+-------------
 id     | integer               |           |          |         | plain    |
 name   | character varying(20) |           |          |         | extended |
View definition:
 SELECT id,
    name
   FROM person;

*Memos:

  • You must set + and <view-name> otherwise the code is not shown.

  • You can omit the schema public..

Or, you can show the code of my_v view with pg_views as shown below:

postgres=# SELECT definition FROM pg_views WHERE viewname = 'my_v';
   definition
-----------------
  SELECT id,    +
     name       +
    FROM person;
(1 row)

Or, you can show the code of my_v view with information_schema.views as shown below:

postgres=# SELECT view_definition FROM information_schema.views WHERE table_name = 'my_v';
 view_definition
-----------------
  SELECT id,    +
     name       +
    FROM person;
(1 row)
Adalbert answered 15/1, 2024 at 22:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.