PQconnectdb set search_path (schema) while connecting - postgresql

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

Related

Error creating a connection to Postgresql while preparing konga database

After installing Konga, we are trying to prepare Konga database on the already running Postgresql database. by using suggested command i.e.
node ./bin/konga.js prepare --adapter postgres --uri postgresql://localhost:5432/konga
But we are facing the error as below:
Error creating a connection to Postgresql using the following settings:
postgresql://localhost:5432/konga?host=localhost&port=5432&schema=true&ssl=false&adapter=sails-postgresql&user=postgres&password=XXXX&database=konga_database&identity=postgres
* * *
Complete error details:
error: password authentication failed for user "root"
error: A hook (`orm`) failed to load!
error: Failed to prepare database: error: password authentication failed for user "root"
We even created the schema konga_database manually and have tried several variations for prepare command but no fate
node ./bin/konga.js prepare --adapter postgres --uri postgresql://kong:XXXX#localhost:5432/konga_database
node ./bin/konga.js prepare --adapter postgres --uri postgresql://kong#localhost:5432/konga
node ./bin/konga.js prepare --adapter postgres --uri postgresql://kong#localhost:5432/konga_database
Below is config/connections.js
postgres: {
adapter: 'sails-postgresql',
url: process.env.DB_URI,
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD || 'XXXX',
port: process.env.DB_PORT || 5432,
database: process.env.DB_DATABASE ||'konga_database',
// schema: process.env.DB_PG_SCHEMA ||'public',
// poolSize: process.env.DB_POOLSIZE || 10,
ssl: process.env.DB_SSL ? true : false // If set, assume it's true
},
Below is .env file configuration
PORT=1337
NODE_ENV=production
KONGA_HOOK_TIMEOUT=120000
DB_ADAPTER=postgres
DB_URI=postgresql://localhost:5432/konga
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=XXXX
KONGA_LOG_LEVEL=info
TOKEN_SECRET=
kong and postgresql are already running on the AWS linux AMI 2 server on there respective ports i.e. 8443 & 5432
Please help us to prepare DB and start konga service. Also. let us know in case you need more info.
Node v: v12.19.0
NPM v: 6.14.8
Regards
Nitin G
Maybe I overlooked it, but what version of PostreSQL are you using?
Konga is not able to support postgresql 12:
https://github.com/pantsel/konga/issues/487
Have you tried like this?
.env
DB_URI=postgresql://userdb:passworddb#localhost:5432/kongadb
I tried on Postgresql 9.6
https://www.rosehosting.com/blog/how-to-install-postgresql-9-6-on-ubuntu-20-04/

Postgres - dblink_connect how to set timeout

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.

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.

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

PostgreSQL: difficulties with basic pglogical configuration

I find official pglogical documentation very confusing. There are too many things that are not clear. Perhaps someone who configured pglogical before could explain how to setup a basic logical replication.
There are two PostgreSQL 9.5 instances - 10.128.0.8 (archlinux1) and 10.128.0.9 (archlinux2). Extension is already installed, CREATE EXTENSION succeeded. There is a table on each instance:
create table test (k text primary key, v text);
I would like to replicate it from archlinux1 to archlinux2.
According to the documentation I should create a provider node:
SELECT pglogical.create_node(
node_name := 'provider1',
dsn := 'host=providerhost port=5432 dbname=db'
);
Should it be executed on master? Should providerhost be 127.0.0.1 or 10.128.0.8? Currenlty replication is allowed only from localhost (accordingly to docs) - should it be changed? My best guess - it should be executed on master like this:
SELECT pglogical.create_node(
node_name := 'provider1',
dsn := 'host=127.0.0.1 port=5432 dbname=eax'
);
create_node
-------------
2976894835
(1 row)
Next:
SELECT pglogical.replication_set_add_all_tables('default', ARRAY['public']);
Should it be executed on master, replica, or both? My best guess - only on master:
SELECT pglogical.replication_set_add_all_tables('default', ARRAY['public']);
replication_set_add_all_tables
--------------------------------
t
(1 row)
Next:
SELECT pglogical.create_node(
node_name := 'subscriber1',
dsn := 'host=thishost port=5432 dbname=db'
);
Apparently it should be executed on replica:
SELECT pglogical.create_node(
node_name := 'subscriber1',
dsn := 'host=127.0.0.1 port=5432 dbname=eax'
);
create_node
-------------
330520249
(1 row)
Next step:
SELECT pglogical.create_subscription(
subscription_name := 'subscription1',
provider_dsn := 'host=providerhost port=5432 dbname=db'
);
Best guess - execute it on replica like this:
SELECT pglogical.create_subscription(
subscription_name := 'subscription1',
provider_dsn := 'host=10.128.0.8 port=5432 dbname=eax'
);
ERROR: could not connect to the postgresql server: could not connect to server: Connection refused
Is the server running on host "10.128.0.8" and accepting
TCP/IP connections on port 5432?
DETAIL: dsn was: host=10.128.0.8 port=5432 dbname=eax
Oops. OK, modifying pg_hba.conf and postgresql.conf properly on master:
# pg_hba.conf
host all all 10.128.0.0/16 md5
# postgresql.conf
listen_addresses = 'localhost,10.128.0.8
Still no luck:
# SELECT pglogical.create_subscription(
subscription_name := 'subscription1',
provider_dsn := 'host=10.128.0.8 port=5432 dbname=eax user=eax password=qwerty'
);
ERROR: could not connect to the postgresql server in replication mode: FATAL: no pg_hba.conf entry for replication connection from host "10.128.0.9", user "eax", SSL off
DETAIL: dsn was: host=10.128.0.8 port=5432 dbname=eax user=eax password=qwerty
Adding to pg_hba.conf on master:
host replication eax 10.128.0.0/16 md5
On replica (success!):
SELECT pglogical.create_subscription(
subscription_name := 'subscription1',
provider_dsn := 'host=10.128.0.8 port=5432 dbname=eax user=eax password=qwerty'
);
create_subscription
---------------------
1763399739
(1 row)
Now on master:
eax=# insert into test values ('aaa', 'bbb');
INSERT 0 1
eax=# select * from test;
k | v
-----+-----
aaa | bbb
(1 row)
On replica:
eax=# select * from test;
k | v
---+---
(0 rows)
Naturally it didn't work. Nothing helpful in logs. Any advice?
UPD: I also created a corresponding issue in pglogical issue tracker.
Perhaps this is a better guide: http://bonesmoses.org/2016/10/14/pg-phriday-perfectly-logical/ - at least it tells you which nodes to execute each command on.
Depesz published an excellent blog post about pglogical configuration https://www.depesz.com/2016/11/08/major-version-upgrading-with-minimal-downtime/
When creating nodes you need to specify an external IP (not localhost)
On the provider
SELECT pglogical.create_node(
node_name := 'provider1',
dsn := 'host=subscriberhost port=5432 dbname=db'
);
On the subscriber
SELECT pglogical.create_node(
node_name := 'subscriber1',
dsn := 'host=providerhost port=5432 dbname=eax'
);