I have already configured one of our server with pg-bouncer.pg-bouncer is listening to 6432 port and postgres is listening to 5432 port. Using like below command I can access pg-bouncer which is giving proxy to postgres 5432 port.
psql -p 6432 -U user db1
But my question is, how to understand pg-bouncer is working properly with postgres? I mean, when there will be any database request may be read/write will pg-bouncer work automatically now?
Suppose you launched pgbouncer with config.ini like below.
[databases]
template1 = host=localhost port=5432 dbname=template1
[pgbouncer]
listen_port = 6432
listen_addr = localhost
auth_type = md5
auth_file = userlist.txt
logfile = pgbouncer.log
pidfile = pgbouncer.pid
admin_users = someuser
You can connect to pgbouncer instead of directly to the PostgreSQL server:
$ psql -p 6432 -U someuser template1
You application can connect to pgbouncer port 6432 instead of PostgresSQL port 5432, e.g. for java application
jdbc:postgresql://server-name:6432/database-name
Related
Some Postgres connection options are usually specified in the connection string. For example, sslmode=require can be set with the following connection string
postgresql://postgres:postgres#localhost:5432/postgres?sslmode=require
But psql --help doesn't provide any information on how to set something like that.
You can pass in a conninfo string or URI. The two examples below are equivalent.
psql postgresql://postgres:postgres#localhost:5432/postgres?sslmode=require
psql "sslmode=require" -U postgres -h localhost -p 5432 -d postgres
Multiple options are space delineated.
psql "dbname=postgres sslmode=require" -U postgres -h localhost -p 5432
See The Connection URI Parameter Key Words documentation for other options.
You can also inspect the current connection info using the \conninfo meta command
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "localhost" at port "5432".
The online documentation of psql goes into more detail under the
Connecting to a Database section.
I installed and started my Postgres database with brew (on my Mac). I also defined an entry in my /etc/hosts file (I tried both with 127.0.0.1 postgres and with postgres).
However, when I try
psql -h postgres -U postgres -p 5432
I cannot connect
psql: could not connect to server: Connection refused.
However, when I try with
psql -h localhost -U postgres -p 5432
I can connect. What is needed to be able to connect with: psql -h postgres -U postgres -p 5432
Make sure your PostgreSQL server is willing to accept tcp/ip connections on port 5432.
In your PostgreSQL configuration file check these values.
listen_addresses = '*'
port = 5432
I've install pgbouncer-1.7.2 on the same Linux server as PostgreSQL-9.6 database.
When I try to connect to my local database using pgbouncer, database ask to enter password. For example:
psql -p 6432 -U postgres -d mydb10 -h localhost
**Password for user postgres:**
Login is OK after I enter the password.
The same direct request run without password:
psql -p 5432 -U postgres -d mydb10 -h localhost
psql (9.6.5)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
mydb10=# \q
Pgbouncer settings (not all of them):
[databases]
* = host=127.0.0.1 port=5432 auth_user=postgres pool_size=20
[pgbouncer]
listen_addr = *
listen_port = 6432
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
;; auth_query = SELECT usename, passwd FROM pg_shadow WHERE usename=$1
PostgreSQL settings (not all of them):
port = 5432
All METHOD in pg_hba.conf are set to 'trust'.
If I set auth_type = trust, connection via pgbouncer do not require password.
What is interesting is that the same configuration when pgbouncer installed on separate server, has no such problem.
Pleas, let me know if you have any idea how to fix this.
Seems that auth_type = trust is a way to go if you want connection without password check (as you have configured trust in pg_hba.conf)
Regarding
What is interesting is that the same configuration when pgbouncer installed on separate server, has no such problem.
Maybe /etc/pgbouncer/userlist.txt on that different server differs?
PS also note that from version 1.7 onward, pgbouncer supports auth_type = hba that you might find suitable.
PPS your problem with access through different pgbouncer instances might be because of different .pgpass files, which is used by psql
I'm trying to migrate data from old server to new server, both of them used ubuntu server.
I have used this command:
pg_dump -C -h localhost -U localuser dbname | psql -h remotehost -U remoteuser dbname
but it throw:
psql: could not connect to server: Connection timed out
Is the server running on host "X.X.X.X" and accepting
TCP/IP connections on port 5432?
I have checked postgresql conf on new server, running on 5432, listen..="*"
Anything wrong?
Thanks all !
I want to remotely connect to a Postgres instance. I know we can do this using the psql command passing the hostname
I tried the following:
psql -U postgres -p 5432 -h hostname
I modified the /etc/postgresql/9.3/main/pg_hba.conf file on the target machine to allow remote connections by default
I added the following line to the file
host all all source_ip/32 trust
I restarted the cluster using
pg_ctlcluster 9.2 mycluster stop
pg_ctlcluster 9.2 mycluster start
However, when I try to connect from the source_ip, I still get the error
Is the server running on host "" and accepting TCP/IP connections on port 5432?
What am I doing wrong here?
I resolved this issue using below options:
Whitelist your DB host from your network team to make sure you have access to remote host
Install postgreSQL version 4 or above
Run below command:
psql -h <REMOTE HOST> -p <REMOTE PORT> -U <DB_USER> <DB_NAME>
psql -h <IP_Address> -p <port_no> -d <database_name> -U <DB_username> -W
-W option will prompt for password. For example:
psql -h 192.168.1.50 -p 5432 -d testdb -U testuser -W
I figured it out.
Had to set listen_addresses='*' in postgresql.conf to allow for incoming connections from any ip / all ip
Step Wise below
Opening the Port - Make sure the PSQL Port is open to all remote connections or connections from a specific set of IPs as per your requirement. PSQL, in general, runs at port 5432, and it is configurable, so expose relevant Port accordingly.
Update Remote Server PSQL Configuration - Set listen_addresses = '*' in postgresql.conf file, path in general is /etc/postgresql/psql_version/main/postgresql.conf
Connect remotely - psql -U <db_username> -h <IP_address> - in case psql is running on a port other than 5432 on the remote server, specify port by adding -p <port_number>
A little plus below -
In case the IP has been mapped to a domain name, you can connect by replacing <IP_address> with <host_name>. To do this, add a new connection rule in pg_hba.conf file
Note -
All above explained can cause security issues - best practice always is to either keep your psql port closed, or only allow a list of IPs to connect through the port.
Note that "ident" in pg_hba.conf requires a "ident server" to be running on the client.