So this is super strange. I'm trying to do a simple select using dblink as such:

SELECT * FROM dblink('dbname=my_db_name, user=my_user, password=password, hostaddr=127.0.0.1', 'SELECT action, object, created_at, id FROM my_table') AS de(ACTION VARCHAR, OBJECT VARCHAR, created_at TIMESTAMP, id INT)

And I immediately am getting an error message of:

PG::SqlclientUnableToEstablishSqlconnection: ERROR: could not

establish connection DETAIL: FATAL: role "my_user," does not exist

But if I connect to psql locally and print out the list of users using \du you'll see it's listed:

                               List of roles
   Role name   |                   Attributes                   | Member of 
---------------+------------------------------------------------+-----------
 MyName        | Superuser, Create role, Create DB, Replication | {}
 my_user       | Create DB                                      | {}

I'm really at a loss as to how to fix this, googling hasn't helped me much at all either. Any thoughts as to why it's giving me this error message?

When I connect using my superuser account I don't need to specify a password and the dblink runs fine, so I'm quite confused. Here is by pg_hba.conf:

local   all             all                                     trust
local   all             all                                     md5

host    all             all             192.168.33.1/24         trust

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             127.0.0.1/32            md5

# IPv6 local connections:
host    all             all             ::1/128                 trust
host    all             all             ::1/128                 md5

Thanks in advance!

有帮助吗?

解决方案

Try removing the commas from the connection string.

SELECT * FROM dblink('dbname=my_db_name  user=my_user password=password hostaddr=127.0.0.1', 'SELECT action, object, created_at, id FROM my_table') AS de(ACTION VARCHAR, OBJECT VARCHAR, created_at TIMESTAMP, id INT)

This should fix the issue where it tries to authenticate as my_user,.

Secondly, your pg_hba configuration is an issue. Because you have the 'trust' method first, it will be used. Either try placing the 'md5' entries before the 'trust' entries, or remove the 'trust' entries. (Set a password for your own postgres account first, so that you can still authenticate once passwords are required.)

The reason for the second issue is that if 'trust' is enabled for the hostname you're connected from, it will authenticate the user without requiring a password, and since non-superusers cannot connect without a password, it will fail to connect.

Finally, the user running the query that calls dblink must be authenticated using a password. So if you're using 'trust' authentication to connect to the database, then running dblink, you will also get an error. To fix this, change to md5 authentication and connect using a password.

其他提示

You're using commas in the connection string. The error message says:

FATAL: role "my_user," does not exist

Which indicates the exact error in this case. This is not the way do do it. You should only space separate the items in the connection string like:

SELECT * FROM dblink('dbname=my_db_name user=my_user password=password hostaddr=127.0.0.1', 'SELECT action, object, created_at, id FROM my_table') AS de(ACTION VARCHAR, OBJECT VARCHAR, created_at TIMESTAMP, id INT)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top