# Using a PostgreSQL Foreign Data Wrapper to Query and Join Redis Data

DevFeed: [Using a PostgreSQL Foreign Data Wrapper to Query and Join Redis Data](<https://devfeed.tech/articles/redis-in-my-postgres-41120.md>)

Original publisher: [Read original article](<https://www.craigkerstiens.com/2012/10/18/connecting_to_redis_from_postgres/>)

Author: Map

Published: 2012-10-18T20:55:56Z

Content type: tutorial

Language: en

Sources: [Craig Kerstiens](<https://devfeed.tech/sources/craig-kerstiens.md>)

Topics: [Redis](<https://devfeed.tech/topics/redis.md>), [Databases](<https://devfeed.tech/topics/databases.md>), [SQL](<https://devfeed.tech/topics/sql.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [database](<https://devfeed.tech/tags/database.md>), [development](<https://devfeed.tech/tags/development.md>), [git](<https://devfeed.tech/tags/git.md>), [github](<https://devfeed.tech/tags/github.md>), [postgres](<https://devfeed.tech/tags/postgres.md>), [psql](<https://devfeed.tech/tags/psql.md>), [redis](<https://devfeed.tech/tags/redis.md>), [sql](<https://devfeed.tech/tags/sql.md>)

## AI overview

This tutorial explains how to connect Redis to PostgreSQL using a Foreign Data Wrapper, configure the extension and foreign table, and query or join Redis data with PostgreSQL data.

## Source excerpt

Yesterday there was a post which hit Hacker News that talked about using SQL to access Mongo. While this is powerful I think much of the true value was entirely missed within the post. SQL is an expressive language, though people are often okay with accessing Mongo data through its own ORM. The real value is that you could actually query the data from within Postgres then join across your data stores, without having to do some ETL process to move data around. Think... joining sales data from Postgres with user reviews stored in Mongo or searching for visits to a website (retained in redis) against purchases by user in Postgres. The mechanism pointed out was a MongoDB Foreign Data Wrapper. A Foreign Data Wrapper or FDW essentially lets you connect to an external datastore from within a Postgres database. In addition to the Mongo FDW released the other day there's many others. For example Postgres 9.0 and up ships with one called db_link, which lets you query and join across two different Postgres databases. Beyond that there's support for a variety of other data stores including some you may have never expected: Redis Textfile MySQL Oracle ODBC LDAP Twitter More Lets look at actually getting the Redis one running then see what some of the power of it really looks like. First we have to get the code then build it: git clone git://github.com/antirez/hiredis.git cd hiredis make sudo make install Then download the FDW from PGXN PATH=/Applications/Postgres.app/Contents/MacOS/bin/:$PATH USE_PGXS=1 make sudo PATH=/Applications/Postgres.app/Contents/MacOS/bin/:$PATH USE_PGXS=1 make install Now you'll want to connect to your Postgres database, using psql and enable the extension, and finally create your foreign table to your redis server. This is assuming a locally running redis, though you could connect to a remote just as easily: CREATE EXTENSION redis_fdw; CREATE SERVER redis_server FOREIGN DATA WRAPPER redis_fdw OPTIONS (address '127.0.0.1', port '6379'); CREATE FOREIGN TAB