Database Reference
In-Depth Information
Next, establish at least one user for your FDW. All users that connect to your server
should be able to access the Google search server, so here we create one for the entire
public
group:
CREATE
USER
MAPPING
FOR
public
SERVER
www_fdw_server_google_search
;
Now create your foreign table, as shown in
Example 10-4
.
Example 10-4. Make a foreign table from Google
CREATE
FOREIGN
TABLE
www_fdw_google_search
(
q
text
,
GsearchResultClass
text
,
unescapedUrl
text
,
url
text
,
visibleUrl
text
,
cacheUrl
text
,
title
text
,
content
text
)
SERVER
www_fdw_server_google_search
;
The user mapping doesn't assign any rights. You still need to grant rights before being
able to query the foreign table:
GRANT
SELECT
ON
TABLE
www_fdw_google_search
TO
public
;
Now comes the fun part. We search with the term
New in PostgreSQL 9.4
and mix in
a bit of regular expression goodness to strip off HTML tags:
SELECT
regexp_replace
(
title
,
E
'(?x)(< [^>]*? >)'
,
''
,
'g'
)
As
title
FROM
www_fdw_google_search
where
q
=
'New in PostgreSQL 9.4'
LIMIT
2
;
VoilĂ ! We have our response:
title
----------------------------------------------------
What's new in PostgreSQL 9.4 - PostgreSQL wiki
PostgreSQL: PostgreSQL 9.4 Beta 1 Released
(2 rows)