We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Fun with Foreign Data Wrappers (FDW) and PostgreSQL

00:00

Formal Metadata

Title
Fun with Foreign Data Wrappers (FDW) and PostgreSQL
Title of Series
Number of Parts
295
Author
Contributors
License
CC Attribution 3.0 Germany:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor.
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
With Foreign Data Wrappers (FDW) you can access remote objects from SQL databases. This feature is part of the SQL/MED (SQL Management of External Data) specification that was added to the SQL standard. With PostgreSQL you can make use of this great functionality. You can access different remote datasources via FDW for example an ORACLE or MySQL database, JSON- or CSV-file, sources via ODBC and even twitter. The list of FDW is very long: https://wiki.postgresql.org/wiki/Foreign_data_wrappers There are also Geospatial Wrappers for GDAL/OGR, GeoJSON and OpenStreetMap. You can access your geospatial data for example geopackage or OGC WFS via FDW. That is easy and great fun. Some of the wrappers support only read others support read/write access. The presentation will give an overview on the possibilities of Foreign Data Wrappers and show how to setup a connection to remote data using some examples with geospatial data.
Keywords
129
131
137
139
Thumbnail
28:17
MappingMultiplication signMereologyRepetitionPerspective (visual)Projective planeInformationFamilyWrapper (data mining)Lecture/Conference
Musical ensembleSource codeDatabaseSource codeWrapper (data mining)Computer animationLecture/Conference
Wrapper (data mining)Musical ensembleWikiPresentation of a groupLocal ringMusical ensembleCasting (performing arts)RepetitionCartesian coordinate systemWeb 2.0DatabasePresentation of a groupWrapper (data mining)CodeGastropod shellSlide ruleMereologyComputer animation
Convex hullPresentation of a groupWrapper (data mining)DatabaseMultiplication signFunctional (mathematics)Extension (kinesiology)Computer animationXML
GeometryWrapper (data mining)Extension (kinesiology)DatabaseFunctional (mathematics)Extension (kinesiology)CASE <Informatik>Point (geometry)Multiplication signBitBuildingType theoryJSONXML
Wrapper (data mining)Link (knot theory)Menu (computing)Data managementStandard deviationDatabaseSalem, IllinoisData typeExtension (kinesiology)BitDatabaseSource code
Data managementStandard deviationDatabaseWikiWrapper (data mining)MassGeneric programmingRevision controlSource codeSinc functionWritingWikiStandard deviationRead-only memoryConnected spaceDifferent (Kate Ryan album)Right angleFunctional (mathematics)Physical systemMultiplication signXMLComputer animation
Arithmetic meanWrapper (data mining)DatabaseGeneric programmingMassEmulationMaxima and minimaProjective planeFunctional (mathematics)Connected spaceQuicksortLecture/Conference
Wrapper (data mining)Source codeDatabaseTwitterWikiComputer networkExtension (kinesiology)Projective planeComputer fileSource codeElectronic mailing listWikiDatabaseWrapper (data mining)XMLComputer animation
Computer networkExtension (kinesiology)Structural loadServer (computing)Table (information)Wrapper (data mining)DatabaseDifferent (Kate Ryan album)TwitterProjective planeRevision controlLocal ringStructural loadConnected spaceExtension (kinesiology)Process (computing)DatabaseComputer animation
Table (information)Extension (kinesiology)Structural loadServer (computing)Wrapper (data mining)DatabaseRepetitionComputer configurationLocal ringTexture mappingLimit (category theory)PolygonDistanceState of matterDomain nameNumbering schemeMacro (computer science)LoginMenu (computing)MaizeBinary fileEvent horizonQuery languageSummierbarkeitElectronic visual displayLemma (mathematics)User interfaceExtension (kinesiology)CASE <Informatik>Self-organizationTable (information)Computer configurationWrapper (data mining)Query languageSource codeConnected spaceDatabaseServer (computing)MappingLine (geometry)CuboidService (economics)RepetitionForm (programming)Grass (card game)Group actionFunctional (mathematics)Network topologyLimit setComputer animationXML
Table (information)1 (number)Utility softwareMountain passBitMappingTable (information)Computer fileDatabasePasswordComputer programmingVideo gamePlanningDifferent (Kate Ryan album)Extension (kinesiology)
Server (computing)Computer filePasswordWrapper (data mining)Electric currentExtension (kinesiology)Computer configurationComputer filePasswordDatabaseComputer programmingXMLUML
RootComputer fileForm (programming)Extension (kinesiology)Mereology
Server (computing)RootPasswordComputer configurationComputer fileTable (information)Wrapper (data mining)Extension (kinesiology)Computer fileTable (information)SoftwareOracleXMLLecture/Conference
Server (computing)Table (information)Extension (kinesiology)Structural loadWrapper (data mining)OracleDatabaseTexture mappingPasswordComputer configurationState observerIntegerDatabaseConnected spaceExtension (kinesiology)OracleOrder (biology)Computer animationJSON
Texture mappingServer (computing)Table (information)Computer configurationPasswordState observerOracleWrapper (data mining)Bounded variationIntegerExtension (kinesiology)Table (information)Computer configurationCASE <Informatik>BitMappingConnected spaceSheaf (mathematics)Different (Kate Ryan album)JSON
Wrapper (data mining)Table (information)Stack (abstract data type)DatabaseFile formatShape (magazine)Installation artInformationConnectionismSet (mathematics)Pauli exclusion principleServer (computing)Extension (kinesiology)Structural loadTable (information)Metropolitan area networkFile formatGoogolElectronic mailing listRamsey theoryDot productMassCombinational logicDatabaseMereologyPhysical systemVideo gameQuicksortRevision controlCompilation albumComputer fileWrapper (data mining)Level (video gaming)Open setGeometryShape (magazine)Vector spaceInformationComputer animation
Inflection pointPoint (geometry)State of matterFibonacci numberWrapper (data mining)Server (computing)Computer configurationFile formatSource codeTable (information)GeometryIntegerScale (map)RankingAnalytic continuationShape (magazine)UnicodeCodierung <Programmierung>Client (computing)InformationLine (geometry)MultiplicationPolygonFinitary relationVapor barrierAddress spaceCountingDigital filterPlanningReading (process)Drop (liquid)InformationWrapper (data mining)Connected spaceNatural numberComputer fileShape (magazine)Table (information)File formatServer (computing)Line (geometry)Codierung <Programmierung>CASE <Informatik>Computer configurationDatabaseStatement (computer science)Sheaf (mathematics)Greatest elementOracleSource codeBitLevel (video gaming)Scaling (geometry)Lie groupDirectory servicePoint (geometry)Parameter (computer programming)Computer animationSource code
CASE <Informatik>Connected spaceSource codeRemote procedure callFile formatComputer configurationRevision controlInformationAsynchronous Transfer ModeBlock (periodic table)
Sanitary sewerMessage passingMoving averageServer (computing)Configuration spaceClient (computing)Set (mathematics)Asynchronous Transfer ModeError messageMessage passingFlow separationLecture/Conference
Wrapper (data mining)Server (computing)Client (computing)Message passingSet (mathematics)Message passingRepetitionMixed realityWrapper (data mining)JSONXMLUMLLecture/Conference
Interface (computing)Line (geometry)Source codeElectronic mailing listMereologyDatabase transactionNatural numberComputer animationLecture/Conference
Source codeBitConnected spaceMathematicsNumbering schemeLecture/Conference
Numbering schemeConnected spaceSource codeoutputLecture/Conference
Wrapper (data mining)CodeLine (geometry)Connected spaceWindowType theoryComputer animationXML
Server (computing)Directory serviceLocal ringType theoryConnected spaceClient (computing)MappingScripting languageAuthenticationWindowLoginSolitary confinementOffice suiteMehrplatzsystemLecture/Conference
MathematicsCASE <Informatik>Computer virusService (economics)Software developerNumbering schemeLecture/Conference
Wrapper (data mining)Wrapper (data mining)CASE <Informatik>Identity managementGoodness of fitModule (mathematics)DatabaseAnalytic setImplementationAddress spaceTable (information)Regular graphSource codeNumberEndliche ModelltheorieMassPhysical lawService (economics)XMLComputer animationLecture/Conference
Staff (military)Single-precision floating-point formatDatabaseService (economics)Computer animation
Transcript: English(auto-generated)
Okay, yeah, after hearing information from the project itself, my talk is more from the user perspective, I'm using PostGIS since a long time and project, and I discovered
for a data wrapper some time ago and thought that would be a great thing to talk about. And yeah, maybe I have to tell you, I have no maps, no animations in the talk, so we will see some SQL, and I wanted to extend the fun part of it, but maybe
foreign data wrapper are fun themselves, so you will see. So the goal about foreign data wrapper is to get access to other sources via foreign data wrapper, and when I do courses, people complain and they ask, how can I get that from one database to another, and I would like to join them and combine the data, and yeah, we will see how this will work.
So I came from Germany, I'm from Workgroup, someone thought I'm Romanian because I had the local team t-shirt, but I'm not, I'm from Bonn and I work at Workgroup, and we do a lot of web.js application where most of our customers use PostgreSQL as database.
Yes, and if you want to try out foreign data wrapper, I will be at the Code Sprint, a community sprint tomorrow, so we could have a look, a closer look. The presentation is made up that you can easily do the steps on your own, you can use OSU Live, that's all the data that I use in the presentation,
and foreign data wrapper are there as well. So my slides, you can use pgAdmin 4 or 3 to do it, you can use it on the command line, but here's only the steps how you can install pgAdmin 4.
So, why shall we use foreign data wrapper? Why is it fun to work with them? Because we have already dblinked maybe to link from one database to another, so who knows dblink in the audience? Some of you, okay.
So this functionality is around quite a long time I think, it's an extension nowadays, and it enables you to go to another database, from your database which you are in, in this case I go from natural to OpenStreetMap, OSM local,
I take the points that we ship with OSU Live and look for cafes, they are Bucharest data, so I want to find out how many cafes, because I like coffee, are in Bucharest, and this is how I can do it with dblink, so I have to define the database I want to connect, and it was a bit of work,
I always have to define which data type the columns are that I want to connect, and then I can go on. Okay, so I found out that there are 70 cafes, no that's not, can't be, but there are some coffees in Bucharest.
So now we have an extended way, because with dblink I only can connect to other PostgreSQL databases, but I want to connect to other data sources in general, and this was implemented in an SQL standard, it's the SQL MED standard, and this is where it is defined how this connection will work.
Online it said this standard is from 2008, in the front data documentation it says 2001, so I'm not sure about it, I will find out, and when you implement this standard you can connect to two different external data sources.
So who implemented this standard? It says that MariaDB implemented it, but they did it not following the standard so strict, so they implemented a connect functionality, and db2 they connected it,
and PostgreSQL, it was invented in 2011 to PostgreSQL with read-only support in version 9.1, and now in 2013 we have write support since version 9.3. There's a long nice wiki page where you can get an overview on foreign data wrapper,
and which projects, which external projects support these functionality. So as you can see here, there are different projects listed here, like Oracle, ODBC, you can connect, you have SQLite, you have Twitter,
you have all sort of connections you can build up. And for every external data it may be a different way of installing it, so you have to check the installation and see how you can get it running. So you have one foreign data wrapper,
so we saw many sources are supported, it was not the whole list, on the wiki page there are some more projects, and for example you can connect to other databases, to other PostgreSQL databases, you can connect to flat files, and maybe the most interesting thing for us is you can connect to other geospatial data sources,
because in a minute I will talk about the OGR foreign data wrapper. And there's a project that helps you to, for example, connect to Twitter. There are different ways of installation, I mentioned this already,
and now we will have a look how, as compared with a DB-Link version, you can do a connection from one to another database with foreign data wrapper. So the process is always the same, you load an extension which enables you to connect to a foreign data source, then you create a foreign server, you create a foreign user,
because the user you use in PostgreSQL is not the same that you may use to connect to an Oracle database or to some other source, it's not always the case that you need this foreign user, but with some of the connections, and then you can start and connect to tables and build up foreign tables,
and have fun and easily work on external data as if the data would be in your database. So let's see the steps, how they look like. Create extension, a PostgreSQL FDW, so this is a foreign data wrapper that is already shipped
on installation from PostgreSQL, so you can use it out of the box without installing it. And here you see how you can create, oh, no, you can't see it so properly, but I hope you can imagine, it says create server,
and you give a name to the server, and then you say which foreign data wrapper you want to use, in my case it's PostgreSQL foreign data wrapper, and then you as well have to define the options, how you can go to this external data source and build up the connection.
For the user mapping it looks like this, you create a user, create user mapping for user, and then define which server this user should be, for which server, and you define the options, so you say, okay, take this user name and this password, and then I go to the other database and you get authenticated.
The next step, you want to create a foreign table, so the first line is only that I want to separate maybe the data from my data, so I created Shema with a different name, and then there's a nice functionality, you can import foreign Shema from the other database,
and in my case I say, okay, import foreign Shema public, so all of the tables that are in the other database in public Shema will be imported, or only the syntax of the tables is imported to my database. When I do this, and I'm not interested in all the tables,
I could limit the import and say, oh, I only want this and that table, or I could say, okay, some tables I don't want to import, and like this you can only get the tables that you are interested in. And then you have access to this data source from your database,
and can easily run a query where you combine both data sources, and here we can see 147 fees in Bucharest. So maybe at the beginning you won't, if you use pgAdmin 3 still,
you may not find the foreign data wrapper sections, because they are not activated by default, you have to go to your options in pgAdmin and activate some of the check boxes, so foreign server, foreign data wrapper, user mapping, and foreign table.
And then you will see them in the tree, so you see foreign tables here in the middle, you see a bit more above the foreign server, and the different server, and also the user mapping. So here's a small example where you can connect to plain files.
So on OSU Live we have a password file for the programs where you need a password, so I would like to have access to this file from my database. So there's an extension, it's called file foreign data wrapper, and you see the password file is on my desktop,
and now I want to create a server, and it's a foreign data wrapper, to access to this file foreign data wrapper, and I don't need a user mapping,
and first I had to create the extension file foreign data wrapper, maybe I missed that in the slides, and then I can import this table as we saw in the example before, and have easily access to the data from this file. Okay, and I talked about proprietary software already,
Oracle I was talking about, or MS SQL, or other databases you can connect, for example Oracle I have an example here, you can create extension Oracle foreign data wrapper,
and if you have the concept it's always very similar, so for every connection, a foreign data wrapper, maybe you have, yeah, the definition is maybe a bit different, so in this case we define an option section, the DB server,
to build up the connection, and we have to create the user mapping, and then we can create our foreign tables. So like this, as well you can combine tables as if they are in your database,
when you connect to the tables on the remote database, the where clause is pushed down to the database, so not all the data is going to be loaded, the push down is already done for the required columns, and you have explained support. But now the fun part starts, we want to get access to geo data,
and there's a foreign data wrapper that was implemented by Paul Ramsey, it's an OGR foreign data wrapper, and it's as we all love and know OGR, Google, it supports a lot of vector formats,
and yeah, you can access the data from your database, so like geo package, WMS, open street map data, as we shape file KMN, and all sorts of data, even WFS3, you can connect to these formats. You can download and compile OGR foreign data wrapper on yourself,
or if you use OGR Live 13, version 13, it's already shipped with OGR Live, and you can start from at once. And there are packages available, so you could install it on your system as well. It's a long list, if you want to have a look at all the formats,
I only have the top of the list, so if you want to know about which formats are supported, you can go to OGR foreign data wrapper info with minus F, and it will give you a list of all the supported formats, and you also see whether they are only readable,
or read write access does exist. And yeah, maybe you have the sticker as well, let's have a look at S3 shape file, as long as it's still quite in use, so it's very easy to get connection to shape files,
so we have this Natural Earth data on OGR Live, and with this command you can ask OGR foreign data wrapper info to list all the data, all the shape, the layers, we say that it's correlated to the shape files that are in this directory,
and then I can define which of these layers I would like to import, so I, or whether I want to import them all like this, I can import all the layer, and then I can create this, no I can, first I can create a connection to this directory, and then I can create a foreign table,
and at the bottom you see in the options section which layer I would like to import in this, connect with this table statement. And then I can work with this table as, with the shape file as, yeah, directly from my database.
If I have encoding problems, there's a solution as well, I could add an option parameter, shape encoding, for example, and then the encoding will be fine. You have the possibility to connect to OpenStreetMap data,
so with the same concept with minus S, you can list all the layers, and then you can decide which of the layers you would like to connect. So you create a foreign server, the server gets a name already, and here you see data source, it's the OpenStreetMap file format,
you always define the format of the data that you would like to connect, and then create your foreign table. The nice thing is that with this command, OGR foreign data wrapper info will write you already the SQL on the command line,
so you can easily copy this definition, go to your database, and then maybe modify it a bit, and then run the SQL there. Okay, so you also have access to explain analyze,
I said it before in the case of Oracle, and here you can see it's a small SQL where I connect to all the points, in the column highway, which are traffic signals, so there are 600 of them,
and when I run it, you can see how the database gets the data, and here you see the foreign scan in the middle. Okay, this was an example for NTF data, how you can connect to NTF data,
then we have a very nice feature, you can connect to WFS, it's supported, it's read only, and here in the first line you see how you would delete a foreign server, so there are some connections to the user, to the table,
so you have to use this case to delete it, and let's see what happens when you connect to a WFS. So in this case, for WFS, you have to define the data source, then the format is WFS, and I think maybe your data, remote data source does not answer,
so you would like to know what happens, why it doesn't answer, and there's this config option, dpl-debug that you can set to on, and like this, you can get in the debug mode the information, maybe there's still an error with the request or with the version,
and like this it will be listed in the debug mode. Okay, so you can see here, you can set show client messages, and set it to debug 2, and like this you will get the debug messages. And then you can, in the select, you can see,
you can communicate with the WFS. And that's it, that's a short introduction into OGR, WFS, and WFS and Gen, oh no, I'm mixed, foreign data, wrapper in general, I hope you like it,
and maybe one of you would like to try the feature, and if you have questions, come around and we can talk. I think we don't have a microphone for questions, so I'm not sure.
Maybe if there's a question I can repeat it. Pardon? I think so, yes. But I'm not sure, I didn't try all of them, and maybe I'm not so connected to every format, but if you have a look at the list,
we can find out. No, I said before, the question was, does WFS, no.
In general, it provides, ah, transactions, I'm not sure, I'm not sure. Maybe, you know? Hi. So I happen to know, the interface itself provides the way to do transactions,
but if the underlying data source doesn't have a way to implement transactions, it just skips them. Any other questions?
Sometimes it's a little bit of a pain if the scheme of the source changes, so you have to drop and create the FTW connection, or is there a better way, like refresh, or? I think you would have to recreate it,
but maybe the RFI knows something more, but yeah. So there's not a permanent connection to the source, I think, on the remote server, so when you try to connect it, it's built up, so if you change the schema in your remote database, maybe it will get an error, but I didn't try it. So since the invention of import foreign schema,
it's much easier, because now you can just import a new one. You can just wipe it and reimport it, and that's two lines of code. Yeah. Any other questions?
Yeah. Is there any magic user mapping type to channel the active directory login on a Windows client through to making a connection to an MS SQL server?
Do you know that? What was the last part? Sorry. To connect to an... I was visiting a client, and they were talking about using the FDW to connect to the local MS SQL server, which uses their logged-in active directory stuff.
I'm not a Windows person, so I'm not really into it, but as far as I could see, you needed to authenticate the Postgres server instead of the local client using it, so if there was a way to channel the authentication
through the FDW user mapping. I don't know about that. Maybe. Yes, there is a way. You can create multiple users, and there are scripts out there to import LDAP for sure into the Postgres.
Yes. Hello. Hello. Do you know of any practical use cases to dislike? I've been using them, and I cannot say I've seen any proper real use, to be honest.
You've seen the schema changes that can bite you and stuff like that. As a developer, I would usually use APIs to, let's say, put data together with the microservices. They're not a silver bullet, but they say you should use APIs as well in there. Why would you even have a foreign data wrapper to files?
I have a nice use case. I have a customer. They have a GeoPortal, and they have lots of source modules, but they have data from a big region, and they have the parcels and the house numbers, and they don't want to update them regularly,
but want to access them via WFS. We thought about, okay, let's connect the WFS, but the search module that we have doesn't support WFS. In that case, we created a foreign data wrapper, and now we talk to this table that we created,
and that will go to the WMS and gives back the address or the parcel number, a parcel. Like that, it was a very good use case for us and stopped us from updating the data and importing them to the database regularly. It was a really good solution for them.
Any other questions? I'll take a second just for another use case. If you've got a bunch of microservices, and once you want to implement analytics on top of that, and you didn't quite design it right,
a quick way is to connect all the databases of all the data services of your microservices into one single database on Postgres, and after that, you've got powerful staff to join them. Other questions?
Thank you very much. That's it.