Cross database connection issue in PostgreSQL - postgresql

Im trying to fetch data from A table to another Database using Forigen Data Wrapper.
It works fine in my system. But the same commands are throwing errors in Remote Server.
CREATE FOREIGN DATA WRAPPER fwd_test VALIDATOR postgresql_fdw_validator;
CREATE SERVER ser_test FOREIGN DATA WRAPPER fwd_test OPTIONS (hostaddr '127.0.0.1', dbname 'database_one', port '5433');
CREATE USER MAPPING FOR postgres SERVER ser_test OPTIONS (user 'postgres', password 'emr123');
SELECT dblink_connect('ser_test');
And server returns the following error
ERROR: could not establish connection
SQL state: 08001
Detail: missing "=" after "ser_test" in connection info string
Same set of lines is working in local instance.

Judging by the docs, dblink_connect() was extended to accept a foreign server name in between 9.2 and 9.3.
I'm guessing (based on the question tag) that your remote server is running 9.1, but your local instance is something more recent.

You need not create your own foreign data wrapper; the extension creates one for you that is called postgres_fdw.
You can of course create a new foreign data wrapper object, but the correct way to do it would be like this:
CREATE FOREIGN DATA WRAPPER fdw_test
HANDLER postgres_fdw_handler
VALIDATOR postgres_fdw_validator;
You forgot the handler, without which the foreign data wrapper does not know where its code is.
Your PostgreSQL 9.1 tag makes no sense, since postgres_fdw was added with version 9.3.

Related

ERROR: connection for foreign table "test_enames" cannot be established

I am getting below error while selecting the foreign table from Postgres & please help me on fixing the issue.
ERROR: connection for foreign table "test_enames" cannot be established
DETAIL: ORA-12154: TNS:could not resolve the connect identifier
specified
SQL state: HV00N
Details
1.I am using Postgres 13 version on Windows 10 64 Bit machine.
2.I installed oracle_fdw-2.3.0-pg13-win64 successfully in my Windows 10 6bit machine.
3.Created system variable for TNS_ADMIN=C:\Oracle\product\12.2.0x64\client_1\network\admin
4.Created below steps successfully.
CREATE FOREIGN DATA WRAPPER oracle_fdw
CREATE SERVER foreign_oracle
TYPE 'Oracle12'
VERSION '12'
FOREIGN DATA WRAPPER oracle_fdw
OPTIONS (dbserver '//vms1.abc.com:1524/ABC00D70');
CREATE USER MAPPING FOR postgres SERVER foreign_oracle
OPTIONS ("user" 'Test', password 'Test1');
CREATE FOREIGN TABLE test_enames(
eno numeric NULL,
ename character varying(100),
eloc character varying(100)
) SERVER foreign_oracle
OPTIONS (table 'TEST_ENAMES');
But still getting error while selecting table , let me know incase if i missed any steps.
Thanks
That seems like an Oracle configuration problem.
To debug this, try running the following as the operating system user that runs the PostgreSQL service:
sqlplus Test/Test1#//vms150.abc.com:1524/ABC00D70
If that doesn't work, start debugging that. That should be easier, since oracle_fdw is not part of the equation then.
If the sqlplus call above works as you intend it to, make sure that the PostgreSQL service has the TNS_ADMIN environment variable set (did you restart the service after setting it?). I am not sure how to check the environment for a running process on Windows, but "Process Explorer" might do the trick.
Besides, you shouldn't use CREATE FOREIGN DATA WRAPPER to create the FDW, but create the extension as specified in the documentation:
CREATE EXTENSION oracle_fdw;
That will create the foreign data wrapper for you.

PostgreSQL odbc_fdw (64bit) Connection to MS Access (64bit)

I'm trying to link existing MS Access 2013 tables into a PostgreSQL 12 Database, both 64bit version and installed on a small network using odbc_fdw. The databases are on different machines on a windows network. I have a system DSN installed and checked (using pyodbc).
In PostgreSQL I am able to create the extension; Foreign Data Wrappers; Foreign Server and Foreign Table, and to create the User Mappings, but when trying to run a query I get the "ERROR: Connecting to the driver". I tried numerous options following the little literature I found without any luck. I can use the ocdb_fwd to connect to a MySQL server straight forward but I could not figure out how to do it with MSAccess.
I would deeply appreciate it if somebody can help me figure out how to connect MSAccess to the fdw if possible.
These are my basic settings in PostgreSQL:
CREATE FOREIGN DATA WRAPPER odbc_data_wrapper
HANDLER public.odbc_fdw_handler
VALIDATOR public.odbc_fdw_validator;
CREATE SERVER odbc_msaccess
FOREIGN DATA WRAPPER odbc_data_wrapper
OPTIONS (dsn 'msaccess');
CREATE USER MAPPING FOR postgre SERVER odbc_msaccess
OPTIONS("ocdb_UID" 'Admin', "ocdb_pwd" '');
CREATE FOREIGN TABLE test(
id integer NOT NULL,
name character varying NOT NULL
)
SERVER odbc_msaccess
OPTIONS (layer 'test',
sql_query 'SELECT id, name FROM test);
DSN: msaccess working. Tested on pyodbc
odbc_data_wrapper: tested just fine connecting a MySql database
The databases are on different machines
Yeah, that's likely not going to work.
PostgreSQL needs direct access to the Microsoft Access Database, so it either has to be on the same machine, or on a network share. But if you're running it on a network share, you need to make sure that the user running PostgreSQL has access to the network share, the DSN is installed on the machine running PostgreSQL, and you're properly referring to the network path.

Possible to pass a Foreign Server connection from DB1 to DB2 to run a query on DB2 with that connection?

In PostgreSQL 9.6, I'm in one database using a Foreign Server connection. But I have a second database which is a common Db with common functions. I'd like to add a common function to that common db where I can pass in a Foreign Server connection and have that function execute the query on the remote server through DBLINK_CONNECT and DBLINK_SEND_QUERY.
I realize I can physically add the foreign server to the common db, however that would not be practical as I'd have to add a new physical foreign server for every new remote server and that could get out of hand. So I'd rather pass in the connection and use DBLINK_CONNECT to create the connection on the fly rather than use a physical.
Is this possible? I have looked around without luck.
Thanks.

Linking Access tables into a PostgreSQL Database using a foreign data wrapper

I'm new to postgres so this problem is probably a relatively easy one for someone else. However, I have spent many frustrating hours trying to figure out the solution. I have an Access Database of metadata that must be kept updated for sending records to other groups. I also have a database using PostgreSQL and PGAdmin which also has these same metadata tables. Currently these tables in the Postgres database get updated manually by exporting the Access tables as excel files, and then importing them into the SQL tables. It's not the most efficient process and could lead to errors in the SQL database if someone forgets to check before running any queries that they are using the most recent data from Access. So I would like to integrate some of the tables from my Access database with my Postgres database.
Originally I tried just installing drivers to export the Access tables directly to Postgres which worked, but not in the way that I wanted since it just brings in a table which I would still need to manually update. From my understanding, I can create a server connection in postgres to access and that would then bring in updated data using a foreign data wrapper.
I tried to use ogr_fdw.
CREATE EXTENSION ogr_fdw;
When I try:
CREATE SERVER metadata
FOREIGN DATA WRAPPER ogr_fdw
OPTIONS (
datasource 'H:\Databases\20170712.accdb',
format 'ODBC' );
I receive: ERROR: unable to connect to data source "H:\Databases\20170712.accdb"
SQL state: HV00D
When I try:
CREATE SERVER metadata
FOREIGN DATA WRAPPER ogr_fdw
OPTIONS (
datasource 'H:\Databases\20170712.accdb',
format 'ACCDB' );
I receive: ERROR: unable to find format "ADDCB"
HINT: See the formats list at http://www.gdal.org/ogr_formats.html.
I also tried MDB and received the same error. However, MDB is the code name given by the website but it says that it needs JDK/JRE to compile and I'm not really sure if that's another type of driver that I would need or what it is.
When I try:
CREATE SERVER metadata
FOREIGN DATA WRAPPER ogr_fdw
OPTIONS (
datasource 'H:\Databases\20170712.mdb',
format 'ODBC' );
I receive: ERROR: unable to connect to data source "H:\Databases\20170712.mdb"
SQL state: HV00D
Hint: Unable to initialize ODBC connection to DSN for DRIVER=Microsoft Access Driver (*.mdb);DBQ=H:\Databases\20170712.mdb,
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
However I thought after looking at the github help page for ogr_fdw didn't need ODBC and special drivers https://github.com/pramsey/pgsql-ogr-fdw/blob/master/FAQ.md.
A lot of this is probably due to my limited knowledge of the terminology when I'm reading through a lot of this stuff. Also my Access database is an .accdb file but since that wasn't working I tried around with mdb and ODBC as the "format" too. If anyone has any suggestions I would greatly appreciate it.
Thanks!

How to create Postgres to Postgres foreign data table

I need to access tables in a remote Postgres database. I spent a couple hours reading through docs last night, and this is what I've come up with.
-- surely this thing is already written somewhere?
CREATE FUNCTION pgsql_fdw_handler()
RETURNS fdw_handler
AS '?????' LANGUAGE C STRICT;
-- surely this thing is already written somewhere?
CREATE FUNCTION pgsql_fdw_validator(text[], oid)
RETURNS void
AS '????' LANGUAGE C STRICT;
CREATE FOREIGN DATA WRAPPER pgsql
HANDLER pgsql_fdw_handler
VALIDATOR pgsql_fdw_validator;
CREATE SERVER products_remote FOREIGN DATA WRAPPER pgsql OPTIONS
(host 'localhost', dbname 'product_db', port '1234');
CREATE USER MAPPING FOR CURRENT_USER
SERVER products_remote;
CREATE FOREIGN TABLE products (
name text,
id integer
) SERVER products_remote;
select * from products;
I can't figure out what to do for the handler and validator functions, do they exist somewhere? Do I need to write them myself? (what would that look like?) And would appreciate a validation of the overall approach.
I did find this contrib module, but it's apparently incomplete.
First, you need to get the pgsql_fdw code. I could not find a tarball so you are stuck with a git clone. To get it:
git clone git://interdbconnect.git.sourceforge.net/gitroot/interdbconnect/interdbconnect
cd interdbconnect/pgsql_fdw
make USE_PGXS=1
make USE_PGXS=1 install;
Then log into pgsql
CREATE EXTENSION pgsql_fdw;
create foreign data wrapper postgresql handler pgsql_fdw_handler;
create server lsmb foreign data wrapper postgresql options (host 'localhost', dbname 'mtech_test', port '5432');
create user mapping for current_user server lsmb;
create foreign table defaults (setting_key text, value text) server lsmb;
select * from defaults;
Then this works for me.
I don't know whether pgsql_fdw was included in contrib in 9.2.