Git Product home page Git Product logo

pg-fdw-demo's Introduction

pg-fdw-demo

A foreign data wrapper(fdw) is an extension available in PostgreSQL that allows you to access a table or schema in one database from another.

In this repo, we are using the Kubernetes to deploy the Postgresql instances.

prerequisites

setup

tl;dr: ./scripts/up.sh

namespace

kubectl create namespace pg-dfw-demo --dry-run=client -o yaml | kubectl apply -f -

postgresql

follow the bitnami postgresql chart to install postgresql

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update bitnami

create instances

helm upgrade --install local-postgresql bitnami/postgresql -n pg-dfw-demo -f postgresql/local.yaml
helm upgrade --install foreign-postgresql bitnami/postgresql -n pg-dfw-demo -f postgresql/foreign.yaml

on local instance

login to the postgresql

kubectl run local-postgresql-client --rm --tty -i --restart='Never' --namespace pg-dfw-demo --image docker.io/bitnami/postgresql:14.5.0-debian-11-r21 --env="PGPASSWORD=demo_password" --command -- psql --host local-postgresql -U postgres -d postgres -p 5432

create a FDW extension and a postgresql foreign server

CREATE EXTENSION postgres_fdw;
CREATE SERVER foreign_pg_svr FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'foreign-postgresql', port '5432', dbname 'postgres');

create the user mapping

CREATE USER MAPPING FOR CURRENT_USER SERVER foreign_pg_svr OPTIONS (user 'foreign_user', password 'foreign_user_password');

create the schema

CREATE SCHEMA foreign_pg;

import the foreign schema

IMPORT FOREIGN SCHEMA public
LIMIT TO (test_table, users)
FROM SERVER foreign_pg_svr
INTO foreign_pg;

operations

on local instance

login to the postgresql

kubectl run local-postgresql-client --rm --tty -i --restart='Never' --namespace pg-dfw-demo --image docker.io/bitnami/postgresql:14.5.0-debian-11-r21 --env="PGPASSWORD=demo_password" --command -- psql --host local-postgresql -U postgres -d postgres -p 5432

list all foreign tables

SET search_path=foreign_pg;
\det;

          List of foreign tables
   Schema   |   Table    |     Server
------------+------------+----------------
 foreign_pg | test_table | foreign_pg_svr
 foreign_pg | users      | foreign_pg_svr
(2 rows)

read from foreign tables

select count(*) from foreign_pg.users;
 count
-------
 10000
(1 row)

write into the foreign tables

insert into foreign_pg.test_table(id) values (42);

cleanup

tl;dr: ./scripts/down.sh

helm uninstall local-postgresql -n pg-dfw-demo
helm uninstall foreign-postgresql -n pg-dfw-demo
kubectl delete pvc --all -n pg-dfw-demo
kubectl delete namespace pg-dfw-demo

references

pg-fdw-demo's People

Contributors

ivanwoo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.