{"id":2556,"date":"2018-05-16T11:45:58","date_gmt":"2018-05-16T08:45:58","guid":{"rendered":"https:\/\/www.fatihacar.com\/blog\/?p=2556"},"modified":"2019-10-23T16:35:30","modified_gmt":"2019-10-23T13:35:30","slug":"how-to-using-dblink-with-dml-operations-in-postgresql-9","status":"publish","type":"post","link":"http:\/\/www.fatihacar.com\/blog\/how-to-using-dblink-with-dml-operations-in-postgresql-9\/","title":{"rendered":"How to Using Dblink With DML Operations in PostgreSQL 9"},"content":{"rendered":"<p>Dblinks provide to connect another databases. Also you can do DML operations with using dblink. I will use dblink to connect two PostgreSQL databases on this post. Also I will tell how to use select, update and delete with dblink.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1512\" src=\"https:\/\/www.fatihacar.com\/blog\/resimler\/Postgres_logo.png\" alt=\"\" width=\"377\" height=\"137\" srcset=\"http:\/\/www.fatihacar.com\/blog\/resimler\/Postgres_logo.png 377w, http:\/\/www.fatihacar.com\/blog\/resimler\/Postgres_logo-300x109.png 300w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/p>\n<p>Firstly, you have to create dblink entension with related user which is using dblink.<\/p>\n<p><strong>Create Dblink Extension<\/strong><\/p>\n<blockquote>\n<pre><code>\r\npostgres@testdb ~]# psql\r\n\r\npsql&gt; create extension dblink\r\n<\/code><\/pre>\n<\/blockquote>\n<p><strong>Dblink Connection Test<\/strong><\/p>\n<blockquote>\n<pre><code>\r\npsql&gt; select dblink_connect('dbname=remotedbname host=remotedatabaseipaddress user=remoteusertoconnectdatabase password=remoteuserpassword');\r\n\r\nIf result is OK, your connection test is successful.\r\n<\/code><\/pre>\n<\/blockquote>\n<p><strong>Select Operation With Dblink<\/strong><\/p>\n<blockquote>\n<pre><code>\r\npsql&gt; select * from dblink('dbname=remotedbname host=remotedatabaseipaddress user=remoteusertoconnectdatabase password=remoteuserpassword','select id,name from remotetable') \r\nas t(id numeric(10,0),name character varying(100));\r\n<\/code><\/pre>\n<\/blockquote>\n<p><strong>Update Operation With Dblink<\/strong><\/p>\n<blockquote>\n<pre><code>\r\npsql&gt; select dblink_exec('dbname=remotedbname host=remotedatabaseipaddress user=remoteusertoconnectdatabase password=remoteuserpassword','update remotetable set name=''Fatih'' where id = 1');\r\n\r\nYou can use dynamic variable to where condition.\r\n\r\npsql&gt; select dblink_exec('dbname=remotedbname host=remotedatabaseipaddress user=remoteusertoconnectdatabase password=remoteuserpassword','update remotetable set name=''Fatih'' where id = ' || ID || '');\r\n\r\nIf you get an error like \"ERROR: query has no destination for result data, if you want to discard the results of a select, use perform instead.\", you can use perform statement instead of select.\r\n\r\npsql&gt; perform dblink_exec('dbname=remotedbname host=remotedatabaseipaddress user=remoteusertoconnectdatabase password=remoteuserpassword','update remotetable set name=''Fatih'' where id = 1');\r\n\r\n<\/code><\/pre>\n<\/blockquote>\n<p><strong>Delete Operation With Dblink<\/strong><\/p>\n<blockquote>\n<pre><code>\r\npsql&gt; select dblink_exec('dbname=remotedbname host=remotedatabaseipaddress user=remoteusertoconnectdatabase password=remoteuserpassword','delete from remotetable where id = 1');\r\n\r\nYou can use dynamic variable to where condition.\r\n\r\npsql&gt; select dblink_exec('dbname=remotedbname host=remotedatabaseipaddress user=remoteusertoconnectdatabase password=remoteuserpassword','delete from remotetable where id = ' || ID || '');\r\n\r\nIf you get an error like \"ERROR: query has no destination for result data, if you want to discard the results of a select, use perform instead.\", you can use perform statement instead of select.\r\n\r\npsql&gt; perform dblink_exec('dbname=remotedbname host=remotedatabaseipaddress user=remoteusertoconnectdatabase password=remoteuserpassword','delete from remotetable where id = 1');\r\n\r\n<\/code><\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Dblinks provide to connect another databases. Also you can do DML operations with using dblink. I will use dblink to connect two PostgreSQL databases on this post. Also I will tell how to use select, update and delete with dblink. Firstly, you have to create dblink entension with related user which is using dblink. Create&#8230;<\/p>\n","protected":false},"author":37,"featured_media":1512,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,113,7],"tags":[],"class_list":["post-2556","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administration-postgresql","category-errors-and-solutions-postgresql","category-sql-postgresql"],"jetpack_featured_media_url":"http:\/\/www.fatihacar.com\/blog\/resimler\/Postgres_logo.png","jetpack_shortlink":"https:\/\/wp.me\/p39NFI-Fe","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/comments?post=2556"}],"version-history":[{"count":6,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2556\/revisions"}],"predecessor-version":[{"id":2709,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2556\/revisions\/2709"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/media\/1512"}],"wp:attachment":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/media?parent=2556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/categories?post=2556"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/tags?post=2556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}