281

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.

9 Answers 9

344

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
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 @Brain90 is the closest I've found to quick editing view definitions.
Related tip: \dv lists all views
@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?
245
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

5 Comments

cool, it even pretty-prints it! :) the manual says it is deprecated, though... :( thanks!
@elias: just use the version that uses an OID by casting the name to an oid: select pg_get_viewdef('viewname'::regclass, true)
@elias as an alternative to casting, this works too: SELECT pg_get_viewdef(to_regclass('viewname')) (requires at least v9.4).
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.
true means pretty
85
select definition from pg_views where viewname = 'my_view'

6 Comments

thanks for this one.. it allows to access the view definition from my program rather than just from the psql-client.
This has the added benefit in that it works for Amazon Redshift as well.
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 SCHEMA
@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.
If your view is not on the search path, use select definition from pg_views where schemaname = 'my_schema' and viewname = 'my_view'
|
26

If you want an ANSI SQL-92 version:

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

1 Comment

The great thing about information_schema is that unlike pg_catalog it's won't change structure between different postgresql versions.
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

1 Comment

Haha, Just to salute him. he commit a lot to psql in early days. git.postgresql.org/gitweb/…
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

Comments

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.

1 Comment

Important detail. This is annoying because I wanted the original "AS SELECT * FROM ..." statement.
9

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

\dv <VIEWNAME>

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.