PostgreSQL 9.5: Hide password from dblink connection - postgresql

I want to you table which is located in the other database.
I am using the dblink for this.
Procedure:
Step 1: Created extension.
CREATE EXTENSION dblink;
Step 2: Making dblink connection string.
select dblink_connect('con','host=127.0.0.1 dbname=makdb user=postgres password=postgres');
Step 3:
select * from dblink('con','select cola,colb from tbl_test') as tbl(cola int,colb varchar(10));
My Question: How do i hide password in step 2?
By searching i came to know that i need to create .pgpass file. But got stuck in how to create and in which step i need to use that file name.

Install dblink extension:
CREATE EXTENSION dblink;
Install postgres_fdw extension (which can be used to access data stored in external PostgreSQL servers):
CREATE EXTENSION postgres_fdw;
Create a new foreign server connection:
CREATE server myserver foreign data wrapper postgres_fdw
OPTIONS (dbname 'foreign_dbname', host 'foreign_host');
Create a user mapping for the foreign server connection that you recently created and your database.
CREATE USER MAPPING FOR "user_in_current_database"
SERVER myserver OPTIONS (user 'foreign_user', password 'foreign_password');
Select some fields in a remote db with the conexion created. Notice that you does not need use the user and password anyrmore.
SELECT tmp_table.*
FROM dblink(
'myserver',
'
SELECT field1,
field2
FROM table
'
)
AS tmp_table(
field1 TEXT,
field2 BIGINT
);
More info:
https://www.postgresql.org/docs/9.5/postgres-fdw.html
https://www.postgresql.org/docs/current/sql-createserver.html
https://www.postgresql.org/docs/current/sql-createusermapping.html

Related

Setting privileges on foreign table on Postgres

How do foreign table privileges work? A simple example where both source_db and destination_db are Postgres databases.
source_db:
-- create user for user mapping
CREATE USER fdw_user WITH PASSWORD 'secret';
-- create table
CREATE TABLE data (value TEXT);
-- set privileges
GRANT ALL ON TABLE data TO fdw_user;
destination_db:
-- create extension
CREATE EXTENSION postgres_fdw;
-- create server
CREATE SERVER remote_source
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'source.domain.com', dbname 'source_db');
-- create user mapping
CREATE USER MAPPING
FOR PUBLIC
SERVER remote_source
OPTIONS (user 'fdw_user', password 'secret');
-- create foreign table
CREATE FOREIGN TABLE data_from_source(value TEXT)
SERVER remote_source
OPTIONS (table_name 'data');
Now setting privileges for any user in destination_db seems to have no effect, like
GRANT SELECT ON TABLE data_from_source TO localuser;
How can I set privileges on foreign table?
The problem was PgAdmin III. ACL of foreign table changed but PgAdmin did not show it. Psql on command line \dp+ data_from_source shows the ACL as expected.

Create foreign table on file_fdw as non-superuser

I have created a file_fdw extension and a corresponding server as superuser.
CREATE EXTENSION file_fdw;
CREATE SERVER myserver FOREIGN DATA WRAPPER file_fdw;
ALTER SERVER myserver OWNER TO nonsuperuser;
I want a non-superuser nonsuperuser to use this server to create a foreign table
CREATE FOREIGN TABLE test (
a text NULL,
b text NULL
)
SERVER myserver
OPTIONS (filename '/home/me/mycsvfile.csv', format 'csv', header 'true', delimiter ';');
Executing this, leads to `only superuser can change options of a file_fdw foreign table
What can I do to enable nonsuperuser to create foreign tables? If possible I would not mind declaring the options as super user.
Only highly privileged users are allowed to access files on the database server, that's why you need high permissions to create a file_fdw foreign table.
From the error message it becomes clear that you are using an old version of PostgreSQL; on more recent versions, the error message would look like:
only superuser or a member of the pg_read_server_files role may specify the filename option of a file_fdw foreign table
So, as an alternative to dealing out superuser privileges, you may add the user to the pg_read_server_files role.
Upgrade PostgreSQL!

How to add a remote Postgresql db(linked server) to a Postgresql db?

I have a postgresql db at home and one on the cloud. I'd like to add my home db to the cloud db so I can query easily between databases. How can this be done? Without using dblink http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html
My home db will use a dynamic ip provider (can I add a dynamic ip address such as myhomedb.dedyn.io to postgresql settings?)
I'm stating all this in case there are any issues. My home db will only be used to update massive amount of data but isn't mission critical (as we know cloud computing isn't cheap).
Thanks in advance.
Looks like postgres-fdw is the way to go: https://www.postgresql.org/docs/current/postgres-fdw.html
First install the extension:
CREATE EXTENSION postgres_fdw;
Then create a foreign server using CREATE SERVER. In this example we
wish to connect to a PostgreSQL server on host 192.83.123.89
listening on port 5432. The database to which the connection is made
is named foreign_db on the remote server:
CREATE SERVER foreign_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '192.83.123.89', port '5432', dbname 'foreign_db');
A user mapping, defined with CREATE USER MAPPING, is needed as well
to identify the role that will be used on the remote server:
CREATE USER MAPPING FOR local_user
SERVER foreign_server
OPTIONS (user 'foreign_user', password 'password');
Now it is possible to create a foreign table with CREATE FOREIGN
TABLE. In this example we wish to access the table named
some_schema.some_table on the remote server. The local name for it
will be foreign_table:
CREATE FOREIGN TABLE foreign_table (
id integer NOT NULL,
data text
)
SERVER foreign_server
OPTIONS (schema_name 'some_schema', table_name 'some_table');
It's essential that the data types and other properties of the columns
declared in CREATE FOREIGN TABLE match the actual remote table.
Column names must match as well, unless you attach column_name options
to the individual columns to show how they are named in the remote
table. In many cases, use of IMPORT FOREIGN SCHEMA is preferable to
constructing foreign table definitions manually.

Postgresql odbc_fdw connection not working

Am trying to connect postgresql to another databas (MySQL) using ODBC_FDW, Am trying to follow the steps mentioned in the github page as below but after trying the below I keep getting the error
ERROR: Connecting to driver
SQL state: 58000
CREATE EXTENSION odbc_fdw schema td_tci;
CREATE SERVER mysql_server
FOREIGN DATA WRAPPER odbc_fdw
OPTIONS ( dsn 'mysql');
CREATE USER MAPPING FOR postgres
SERVER mysql_server
OPTIONS (odbc_UID 'root', odbc_PWD 'root');
CREATE FOREIGN TABLE td_tci.tsi_lead_queue (
columns...
)
SERVER mysql_server
OPTIONS (
--schema 'tci',
--table 'tsi_lead_queue',
sql_query 'select * from tsi_lead_queue'
);
select * from td_tci.tsi_lead_queue
it's working now, I had to define the DSN as system DSN not User's, after doing that it worked fine

Can't access data from pg_index using Foreign Data Wrapper in postgreSQL 9.3

I implement Foreign data wrapper in postgreSQL 9.3 in another postgreSQL DATABASE as below:
CREATE SERVER app_db
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname 'postgres', host 'localhost');
CREATE USER MAPPING for postgres
SERVER app_db
OPTIONS (user 'postgres', password 'XXXX');
CREATE FOREIGN TABLE location_local
(
id integer,
name character varying
)
SERVER app_db OPTIONS (table_name 'location')
SELECT * FROM location_local;
This all works fine as location table is in public schema. but I also want to access data from pg_catalog. When I follow the same procedure to access the data than it gives me error.
ERROR: relation "public.pg_catalog.pg_index" does not exist
is there any way to access data from catalog using FDW or any other way?
You could try the schema_name option in the foreign table definition:
CREATE FOREIGN TABLE foreign_index (
-- ...
)
SERVER app_db
OPTIONS (
schema_name 'pg_catalog',
table_name 'pg_index'
);
But that might won't work, because pg_catalog is not really a schema, but a system-catalog. If that is the case, you can still use the dblink module to run queries at a foreign database.