diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eaa89e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +logs +db +backup + +*.pid +*.pid.lock +*.iml +.idea + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f112452 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +FROM postgres:12 + +LABEL maintainer='Andrey Solovyev solovieff.nnov@gmail.com' + +RUN apt-get update +RUN apt-get -y install postgresql-server-dev-12 libsybdb5 freetds-dev freetds-common make gcc git libaio1 libaio-dev + +WORKDIR /root + +RUN git clone https://github.com/tds-fdw/tds_fdw.git +WORKDIR tds_fdw +RUN make USE_PGXS=1 +RUN make USE_PGXS=1 install + +WORKDIR /root + +RUN apt-get clean && \ + rm -rf /var/cache/apt/* /var/lib/apt/lists/* + +#ENTRYPOINT ["/docker-entrypoint.sh"] +RUN mkdir /pg +RUN mkdir /backup +RUN mkdir /logs +RUN chown postgres:postgres /logs +RUN chown postgres:postgres /pg +RUN chown postgres:postgres /backup + +WORKDIR /pg + +EXPOSE 5432 +#CMD ["postgres"] diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..2582c24 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,3 @@ +WARNING !!!! All additional components and libraries are provided by their vendors free sources 'as is', +and should be used with respect to their licenses. +--- diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..eb869ad --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +version: '3.1' + +services: + + postgres: + build: . + restart: always + container_name: "postgres" + volumes: + - ./db:/var/lib/postgresql/data/ # persist data even if container shuts down + - ./logs:/logs + - ./init.sql:/docker-entrypoint-initdb.d/init.sql + - ./backup:/backup + ports: + - 5430:5432 + networks: + - share-network + environment: + POSTGRES_PASSWORD: 1234 + #below is not used and should be set directly. + SQL_SERVER_USER: sqlserv_user + SQL_SERVER_PASSWORD: sqlserv_password + SQL_SERVER_HOST: 192.168.0.32 + SQL_SERVER_PORT: 1433 + SQL_SERVER_DB: db_name + SQL_SERVER_SCHEMA: sql_srv_schema_name + command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=../logs +networks: + share-network: diff --git a/init.sql b/init.sql new file mode 100644 index 0000000..666ee0e --- /dev/null +++ b/init.sql @@ -0,0 +1,28 @@ +-- creating new db user +CREATE ROLE db_admin LOGIN PASSWORD 'db_admin'; +-- creating new db +CREATE DATABASE db_name owner db_admin ENCODING 'UTF8'; + +\c db_name + +CREATE EXTENSION tds_fdw; +-- env vars can not be passed here directly +-- TODO: sh script to init with env vars +CREATE SERVER mssql_svr + FOREIGN DATA WRAPPER tds_fdw + OPTIONS (servername '192.168.0.32', port '1433', database 'db_to_use', tds_version '7.1'); + +GRANT USAGE ON FOREIGN SERVER mssql_svr TO db_admin; + +CREATE USER MAPPING FOR db_admin + SERVER mssql_svr + OPTIONS (username 'sql_srv_username', password 'sql_srv_password'); + +CREATE USER MAPPING FOR postgres + SERVER mssql_svr + OPTIONS (username 'sql_srv_username', password 'sql_srv_password'); + +IMPORT FOREIGN SCHEMA sql_srv_schema_name + FROM SERVER mssql_svr + INTO public + OPTIONS (import_default 'true'); diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..59825e0 --- /dev/null +++ b/readme.md @@ -0,0 +1,5 @@ +Postgres 12 with tds_fdw extension to access MSSQL server DB directly using foreign tables. Includes basic commands to create and configure foreign server. + +Created on base of: https://hub.docker.com/r/muritiku/postgres-tds + +See TODO in sql script. You have to manually change SQL server values there. diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..91e895e --- /dev/null +++ b/start.sh @@ -0,0 +1,3 @@ +mkdir db +mkdir logs +docker-compose up --build -d postgres diff --git a/stop.sh b/stop.sh new file mode 100644 index 0000000..685cc77 --- /dev/null +++ b/stop.sh @@ -0,0 +1 @@ +docker-compose down