0

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.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
surya
  • 1
  • 1
  • 5
  • 1
    if 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 Answers1

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