<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://tech.muninn.land/index.php?action=history&amp;feed=atom&amp;title=Create_foreign_data_wrapper_%28Postgresql%29</id>
	<title>Create foreign data wrapper (Postgresql) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://tech.muninn.land/index.php?action=history&amp;feed=atom&amp;title=Create_foreign_data_wrapper_%28Postgresql%29"/>
	<link rel="alternate" type="text/html" href="http://tech.muninn.land/index.php?title=Create_foreign_data_wrapper_(Postgresql)&amp;action=history"/>
	<updated>2026-04-15T19:38:20Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.38.4</generator>
	<entry>
		<id>http://tech.muninn.land/index.php?title=Create_foreign_data_wrapper_(Postgresql)&amp;diff=37&amp;oldid=prev</id>
		<title>Luca: Created page with &quot;Category:postgresql In this guide I&#039;m going to show how to read a remote Postgresql database, from Postgresql.  We will work on database &#039;&#039;&#039;A&#039;&#039;&#039; that will access data of database &#039;&#039;&#039;B&#039;&#039;&#039;.  On database &#039;&#039;&#039;A&#039;&#039;&#039; we need to connect with a user with admin privileges, and install the extension: &lt;syntaxhighlight lang=postgresql&gt;CREATE EXTENSION postgres_fdw;&lt;/syntaxhighlight&gt;  Then we create a &#039;&#039;server&#039;&#039; wich is the link to read remote data. &lt;br&gt;Suppose our database &#039;&#039;&#039;B&#039;&#039;&#039;...&quot;</title>
		<link rel="alternate" type="text/html" href="http://tech.muninn.land/index.php?title=Create_foreign_data_wrapper_(Postgresql)&amp;diff=37&amp;oldid=prev"/>
		<updated>2024-06-18T08:04:16Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&lt;a href=&quot;/index.php/Category:Postgresql&quot; title=&quot;Category:Postgresql&quot;&gt;Category:postgresql&lt;/a&gt; In this guide I&amp;#039;m going to show how to read a remote Postgresql database, from Postgresql.  We will work on database &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; that will access data of database &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039;.  On database &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; we need to connect with a user with admin privileges, and install the extension: &amp;lt;syntaxhighlight lang=postgresql&amp;gt;CREATE EXTENSION postgres_fdw;&amp;lt;/syntaxhighlight&amp;gt;  Then we create a &amp;#039;&amp;#039;server&amp;#039;&amp;#039; wich is the link to read remote data. &amp;lt;br&amp;gt;Suppose our database &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039;...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:postgresql]]&lt;br /&gt;
In this guide I&amp;#039;m going to show how to read a remote Postgresql database, from Postgresql.&lt;br /&gt;
&lt;br /&gt;
We will work on database &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; that will access data of database &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
On database &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; we need to connect with a user with admin privileges, and install the extension:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=postgresql&amp;gt;CREATE EXTENSION postgres_fdw;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we create a &amp;#039;&amp;#039;server&amp;#039;&amp;#039; wich is the link to read remote data.&lt;br /&gt;
&amp;lt;br&amp;gt;Suppose our database &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039; is on a server at address &amp;#039;&amp;#039;&amp;#039;192.168.1.22&amp;#039;&amp;#039;&amp;#039; on port &amp;#039;&amp;#039;&amp;#039;5432&amp;#039;&amp;#039;&amp;#039; and the database is called &amp;#039;&amp;#039;&amp;#039;muninn&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=postgresql&amp;gt;CREATE SERVER muninn_server&lt;br /&gt;
FOREIGN DATA WRAPPER postgres_fdw&lt;br /&gt;
OPTIONS (host &amp;#039;192.168.1.22&amp;#039;, dbname &amp;#039;muninn&amp;#039;, port &amp;#039;5432&amp;#039;);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We have also defined the name &amp;#039;&amp;#039;&amp;#039;muninn_server&amp;#039;&amp;#039;&amp;#039; wich is the link to our remote server &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Now the security. We need to give permission on user on database &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; to read data from database &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039;. But we need to specify credentials to read from database &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&amp;lt;br&amp;gt;Suppose in database &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039; we whant user &amp;#039;&amp;#039;&amp;#039;odin&amp;#039;&amp;#039;&amp;#039; to read data on database &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039;. On database &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039; we need to specify credentials. Suppose our user on database &amp;#039;&amp;#039;&amp;#039;B&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;saturn&amp;#039;&amp;#039;&amp;#039; and the password is &amp;#039;&amp;#039;&amp;#039;aPPlep13&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=postgresql&amp;gt;CREATE USER MAPPING FOR odin&lt;br /&gt;
SERVER muninn_server&lt;br /&gt;
OPTIONS (user &amp;#039;saturn&amp;#039;, password &amp;#039;aPPlep13&amp;#039;);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we need to grant to user &amp;#039;&amp;#039;&amp;#039;odin&amp;#039;&amp;#039;&amp;#039; usage of foreign server &amp;#039;&amp;#039;&amp;#039;muninn_server&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=postgresql&amp;gt;GRANT USAGE ON FOREIGN SERVER muninn_server TO odin;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can connect as user &amp;#039;&amp;#039;&amp;#039;odin&amp;#039;&amp;#039;&amp;#039; to database &amp;#039;&amp;#039;&amp;#039;A&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
I strongly suggest to create an appropriate schema where make remote objects visible:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=postgresql&amp;gt;CREATE SCHEMA rem_muninn;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And now we can grant visibility of remote objects of a specific schema in our new created schema:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=postgresql&amp;gt;IMPORT FOREIGN SCHEMA remote_schema&lt;br /&gt;
FROM SERVER muninn_server INTO rem_muninn;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now in schema rem_muninn we can query every object in remote schema.&lt;/div&gt;</summary>
		<author><name>Luca</name></author>
	</entry>
</feed>