Postgres - dblink_connect how to set timeout - postgresql

I'm using dblink_connect inside a postgres function, like this:
select dblink_connect('gw', 'dbname=databasename port=5432 host=RemoteIP user=username password=pass');
When the remote server is down, this dblink_connect take a long time to return
"ERROR: could not establish connection".
I need to set a timeout to this dblink_connect to return error as quick as possible.
I tried something like this:
begin;
SET session statement_timeout to 5000;
select dblink_connect('gw', 'dbname=databasename port=5432 host=RemoteIP user=username password=pass');
commit;
But not worked.
Any idea that I can set timeout ?

You need to specify the connect_timeout, in the connection string:
dblink_connect('gw', 'connect_timeout=2 dbname=databasename port=5432 host=RemoteIP user=username password=pass')
While it is blocked waiting for the connection, it is in a state where it can't be interrupted, which is why statement_timeout doesn't work.

Related

Can't connect to PostgreSQL database using pgx driver but can using terminal

From code
The code bellow outputs the following:
2022/06/21 16:01:07 Failed to connect to db: failed to connect to 'host=localhost user=postgres database=local': server error (FATAL: Ident authentication failed for user "postgres" (SQLSTATE 28000)) exit status 1
import (
"context"
"log"
"github.com/jackc/pgx/v4"
)
func main() {
dbCtx := context.Background()
db, err := pgx.Connect(
dbCtx,
"postgres://postgres:smashthestate#localhost:5432/local",
)
if err != nil {
log.Fatalf("Failed to connect to db: %v\n", err)
}
defer db.Close(dbCtx)
// do stuff with db...
}
From terminal
However, connection to db is possible from terminal. For example, this command if run with the same parameters (db name, user name, password) will give correct output:
psql -d local -U postgres -W -c 'select * from interest;'
Notes
Command works correctly even if sent by user that is neither root nor postgres.
Authentication method for local connection is set to trust inside pg_hba.conf:
local all all trust
So, what am I missing here? Why everything works fine from the command line but doesn't work from code?
Go's defaults are different from psql's. If no hostname is given, Go defaults to using localhost, while psql defaults to using the Unix domain sockets.
To specify a Unix domain socket to Go, you need to do it oddly to get it to survive URI validation:
postgres://postgres:smashthestate#:5432/local?host=%2Ftmp
Though the end might need to be more like ?host=%2Fvar%2Frun%2Fpostgresql, depending on how the server was configured.
But why are you specifying a password which is not needed? That is going to cause pointless confusion.

PQconnectdb set search_path (schema) while connecting

How do I specify the search_path while connecting to my postgres db using PQconnectdb? I want to set the search_path to my_schema. I'm currently using the following connection command:
PQconnectdb("host=localhost user=my_user password=my_password port=5432 dbname=my_db")
You could add the following to your connection info:
options='-csearch_path=my_schema'
So, it would become:
PQconnectdb("host=localhost user=my_user password=my_password port=5432 dbname=my_db options='-csearch_path=my_schema'")
Reference:
https://www.postgresql.org/message-id/D960CB61B694CF459DCFB4B0128514C20886AD46#exadv11.host.magwien.gv.at

PGBouncer : Cant connect on the right db

I'm actually facing an issue. I've installed pgbouncer on a production server, on which i've a Odoo instance and postgresql as well.
Perhaps :
In my logs, i'm having this :
2018-09-10 16:39:16.389 10123 WARNING C-0x1eb5478:
(nodb)/(nouser)#unix(18272):6432 pooler error: no such database: postgres
2018-09-10 16:39:16.389 10123 LOG C-0x1eb5478: (nodb)/(nouser)#unix(18272):6432 login failed: db=postgres user=oerppreprod
Here is the actual conf of pgbouncer :
pgbouncer_archive = host=127.0.0.1 port=5432 dbname=archive
admin_users = postgres
ignore_startup_parameters = extra_float_digits
With aswell, the default config (i've only added/edited this).
Why is he trying to connect on the postgres database ?
When i go back on the previous conf (without PGBouncer, just swapping from port 6432 to 5432), everything is working ....
Any idea ?
Thanks in advance !
I had the same issue, and in my situation. Maybe it will be usefull to somebody:
I have solved this by a few steps:
At the beginning of every request - your Framework or PDO (or else) running the initial query to check if database you asking is exists in the postgres data to process you request.
I have replaced the part of line "user=project_user password=mytestpassword" from the database section of pgbouncer.ini file. As I tested, if you replace this part - then the pgbouncer will use your userlist.txt file (or your selected auth), in my case, it was the userlist.txt.
Added the line "postgres = host=127.0.0.1 port=5432 dbname=postgres"
[databases]
postgres = host=127.0.0.1 port=5432 dbname=postgres
my_database = host=127.0.0.1 port=5432 dbname=my_database
My userlist.txt file looks like this (I am using auth_type = md5, so my password was in md5):
"my_user" "md5passwordandsoelse"
I have added my admin users to my pgbouncer.ini file:
admin_users = postgres, my_user
After all manipulations I advise you to check from which user u are running queries, by usin this simple query:
select current_user;
At the end, with this query you must to receive you selected username (in my case it was - my_user)
p.s. also I must to mention, that I was using 127.0.0.1 - because my pgbouncer is installed on the same server with postgres.

Specify `statement_timeout` in Postgresql with sqlalchemy?

The following statement_timeout option works on some Postgresql databases and on others, I get Unsupported startup parameter: options. Why?
Is this possibly a difference between Postgres 9.4 and 9.6? This works with the former servers and fails with the latter.
from sqlalchemy import create_engine
# As is: Unsupported startup parameter: options
db_engine = create_engine("postgresql://user:pw#host/database",
connect_args={"options": "-c statement_timeout=1000"})
with db_engine.connect() as db_connection:
print("got it")
Specifically, I get:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) ERROR: Unsupported startup parameter: options
You may have been connecting to those databases via PgBouncer.
If so, add ignore_startup_parameters = options to pgbouncer.ini under the [pgbouncer] section.
From https://www.pgbouncer.org/config.html#ignore_startup_parameters:
By default, PgBouncer allows only parameters it can keep track of in startup packets: client_encoding, datestyle, timezone and standard_conforming_strings. All others parameters will raise an error. To allow others parameters, they can be specified here, so that PgBouncer knows that they are handled by the admin and it can ignore them.
Default: empty
References:
https://github.com/pgbouncer/pgbouncer/issues/295
https://github.com/pgbouncer/pgbouncer/issues/496

postgres dblink_connect ERROR: could not establish connection

I am trying to connect to another server using pgadmin3. This is what I am doing:
CREATE EXTENSION dblink;
select postal.* from dblink('host=<name>.us-west-2.rds.amazonaws.com
user=postgres
password=postgres
dbname=name', 'select * from xwg201703.gc_bgr_postcodes')
AS postal(country varchar(3),
postalcode varchar(4),
town_bul varchar(60),
town_bun varchar(60),
locality_bul varchar(60),
locality_bun varchar(60))
limit 1;
The error I get:
ERROR: could not establish connection
DETAIL: could not connect to server: Connection timed out
Is the server running on host ".amazonaws.com" (...) and accepting
TCP/IP connections on port 5432?
Any input would be appreciated.
you cant use postgres as username on RDS
before creating dblink, check if you can connect at all. eg:
psql -h host=<name>.us-west-2.rds.amazonaws.com -U username -d db_name