I need to write code in Postgres, where I'll read data rows from a view and insert into another table if it does not exist. If data row is present in table then it should get updated with new values.
Asked
Active
Viewed 76 times
0
-
1if you run 9.5 you can use new "upsert" - `INSERT .. ON CONFLICT` – Vao Tsun Jun 21 '16 at 06:31
-
@VaoTsun I am using 9.1 – surya Jun 21 '16 at 06:47
-
well then, give us what code you have so far – Vao Tsun Jun 21 '16 at 06:55
-
Possible duplicate of [How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL?](http://stackoverflow.com/questions/17267417/how-to-upsert-merge-insert-on-duplicate-update-in-postgresql) – Elad Jun 21 '16 at 07:01
-
@VaoTsun I have to write from scratch create function test_stag() returns setof view_name as 'select * from view_name;' language 'sql'; – surya Jun 21 '16 at 07:12
1 Answers
0
Write sql queries to display data in Data Output export the data it's in CSV(comma separated values) file. Then using cmd insert or update table.
Command :- PATH>psql -U postgres -d databaseName -h postgresAddress or PATH>psql dbname
/Copy command/ databaseName=# \copy tableName FROM 'path/filename.csv' DELIMITER ',' CSV

Arbind Kumar
- 23
- 7