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

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

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

If you want an ANSI SQL-92 version:
select view_definition from information_schema.views where table_name = 'view_name';

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:
\sv <VIEWNAME>

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

Related

Need Help for FoxPro Select WHERE works, but Replace ALL FOR Fails?

I have this select statement that works in a FoxPro command window to return the 7 records where the objname is documents-mltry and the word "roster" appears in the descrip memo field:
SELECT objname, descrip FROM archives where UPPER(OBJNAME)==[DOCUMENTS-MLTRY] AND [ROSTER]$UPPER(DESCRIP)
However, when i then try to do a replace all statement to change the objname to "Roster" it fails telling me the "variable roster is not found". Note: the LIKE command is not available in this version of FoxPro.
Can anybody see what i am doing wrong in the replace command below?
REPLACE ALL objname WITH "Roster" FOR UPPER(OBJNAME)==[DOCUMENTS-MLTRY] AND [ROSTER]$UPPER(DESCRIP)
I have tried using ATC command in the replace statement that results in the same variable not found error message:
REPLACE ALL objname WITH "Roster" FOR UPPER(OBJNAME)==[DOCUMENTS-MLTRY] AND ATC("ROSTER", descrip)
Any suggestions would be appreciated!
This may be a WAG but I noticed that you are not using the (optional) alias or workarea in your REPLACE command. If the table you are REPLACEing is not SELECTed and the work area that is selected is another table, then you will receive a variable not found error. To test out this theory, add an "IN archives" to your REPLACE command. That might fix it.
REPLACE ALL objname WITH "Roster" FOR UPPER(OBJNAME)==[DOCUMENTS-MLTRY] AND ATC("ROSTER", descrip) in archives

Find and replace using wildcard in netbeans

How would one go about using replace in netbeans to modify a PHP script with lots of $_POST[].
For better security it would be a good idea to replace all these $_POST[]
with sanitize($_POST[]) where sanitize is a function that sanitizes user input.
So I could use Replace and search for $_POST[''] and replace with sanitize($_POST['']).
But how do you keep the variable name within each $_POST[''] while adding the closing parenthesis?
For example $_POST['name'] and $_POST['action'] need to become sanitize($_POST['name']) and santize($_POST['action']) respectively.
I am not a PHP programmer or a Regex master but in my very limited test this seemed to work.
Select the project and then choose Edit -> Replace. Choose "Regular Expression" in the drop down list, set containing text to \$_POST\[(.*)\]and replace with to sanitize(\$_POST[$1])

Why are values copied from returned grid marked with quotes?

In PgAdmin III, when I copy value from returned grid and paste it into the query text, it appears there in double quotes. But to use it in query, in where clause for example, it has to be marked with apostrophe. So I have to replace quotes with apostrophe. It is rather weird for me that I always have to do so. Why it is designed in such strange way?
This is a kind of stupid theoretical question, I do not expect any practical answers :)
You can customize the quoting character, by following the following steps.
On the PgAdmin-III main window, click on File and then Options
Click on the Query Tool Tab
Change the field called "Result copy quote character" to apostrophe (')
It should work, you may need to close and open PgAdmin-III
EDIT: For Mac OS X the option could be found in pgAdmin3 > Preferences > Query tool > Results grid
For pgAdmin 4 go to File> Preferences> SQL Editor> Results Grid. You can change the Quotation mark to single quote or none.

Tab character when exporting in pgAdmin

I issue a query in pgAdmin and then want to export the result to a file. How can I specify that the column separator should be a tab character?
This comes probably too late for you, but maybe not for others.
You can copy / paste a literal tab character into the (dropdown) text box for "Column separator", like any other valid character, and it will be used. Just tested successfully with pgAdmin 1.14.2.
So far, there is no tab-character option in pgAdmin when you try to export the result to a file.
Refer to: http://www.pgadmin.org/docs/dev/export.html

sql server 2000: Short cut for getting stored proc text

what is the short cut to get entire text of the stored proc in query analyzer. I know that I could highlight table name and hit alt + F1 and get the entire table structure.
It seems to not exist a shorcut with your exact request. If you want to see the complete list of query analyzer shorcuts visit the folowing link:
http://msdn.microsoft.com/en-us/library/aa216992(SQL.80).aspx
Anyway you can modify, customize or create your own shorcuts.
For examle ALT+F1 just executes the "sp_help" stored procedure.
Check the documentation at:
http://msdn.microsoft.com/en-us/library/aa216956(SQL.80).aspx
You should be able to add a shortcut that uses sp_helptext, rather then sp_help