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.
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.
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).
\dv
lists all views –
Fonzie \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 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
select pg_get_viewdef('viewname'::regclass, true)
–
Nicotiana SELECT pg_get_viewdef(to_regclass('viewname'))
(requires at least v9.4). –
Beheld select definition from pg_views where viewname = 'my_view'
select definition from pg_views where schemaname = 'my_schema' and viewname = 'my_view'
–
Eoin pg_get_viewdef()
. –
Defilade If you want an ANSI SQL-92 version:
select view_definition from information_schema.views where table_name = 'view_name';
information_schema
is that unlike pg_catalog
it's won't change structure between different postgresql versions. –
Merrygoround 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
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
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.
In the command line client psql you can use following command:
\dv <VIEWNAME>
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)for
pg_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)
© 2022 - 2025 — McMap. All rights reserved.