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.
Related
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 have Kubuntu 14.10 desktop with PostgreSQL 9.4 database installed.
I have changed the password of the postgres user in the database by executing the SQL:
ALTER USER postgres PASSWORD 'password';
And I can connect to DB server by psql -h localhost -U postgres -W and giving that password but I can also connect without a password requirement by simply psql -h localhost -U postgres.
On the other hand, if I run psql -h 127.0.0.1 -U postgres it prompts me for the password set before.
What is the difference between the localhost and 127.0.0.1 hosts and their login method? Where is it set? I see no localhost-related entries in the pg_hba.conf file.
The behavior you see might be caused by a password file. The password file is conventionally named ~/.pgpass on Unix systems, but a different filename can be given through the PGPASSFILE environmental variable.
I think a password file that contains a line for "localhost", but does not contain a line for "127.0.0.1" will show the behavior you're seeing. My own ~/.pgpass file contains this line.
localhost:*:*:postgres:password
Here's what happens when I try to connect just like you did.
$ psql -h localhost -U postgres
psql (9.3.5)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
sandbox=# \q
$ psql -h 127.0.0.1 -U postgres
Password for user postgres:
Adding the line 127.0.0.1:*:*:postgres:password to ~/.pgpass lets me log in using 127.0.0.1 and no password.
$ psql -h 127.0.0.1 -U postgres
psql (9.3.5)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
sandbox=#
I just set up an Amazon RDS instance. I have a separate application server and I am trying to figure out how to connect to the RDS instance from my EC2 application server. On the Instance page, I have
enbdpoint: mycompany.czdv3mj7ps25.us-west-2.rds.amazonaws.com:5432
I tried to login into to psql using that address but I got
$ psql -h mycompany.czdv3mj7ps25.us-west-2.rds.amazonaws.com:5432 -U myuser -d mydb
psql: could not translate host name "mycompany.czdv3mj7ps25.us-west-2.rds.amazonaws.com:5432" to address: Name or service not known
How do I connect to the instances database? I don't see any other ip addresses in the RDS console.
You have wrong syntax. The correct syntax is:
$ psql --host mycompany.czdv3mj7ps25.us-west-2.rds.amazonaws.com --port 5432 --username myuser --dbname mydb
You have specified port information wrong in you command. the port has to be specified using --port option and not hostname:port
This syntax worked for me in the psql command line:
\connect dbname username hostname port#
If the information is correct it will take a couple seconds to process then it will ask for your password
Also make sure the security group for your instance, the outbound/inbound is setup to allow access from your IP
I am trying to create a connection pool to a PostgreSQL database in Glassfish server.
What I have done the steps as:
I copied the jar of postgresql jdbc jar in full path
C:\Program Files\glassfish-3.1.1\glassfish\domains\domain1\lib\ext
I have specified full path to the jar in the glassfish server admin console:
Pool Name: post-gre-sql_CommonPush_postgresPool
Resource Type: javax.sql.XADataSource
Datasource Classname: org.postgresql.ds.PGSimpleDataSource
In case of additional properties I have specified as:
driverClass:org.postgresql.Driver
URL: jdbc:postgresql://10.137.243.1:5432/CommonPush
portNumber: 5432
databaseName: CommonPush
serverName: 10.137.243.1(My system's IP)
user: postgres
password:
When I ping the above settings I get the error as:
An error has occurred
Ping Connection Pool for post-gre-sql_CommonPush_postgresPool is
Failed. Ping failed Exception - Connection could not be allocated
because: Connection refused. Check that the hostname and port are
correct and that the postmaster is accepting TCP/IP connections.
Please check the server.log for more details.
Ping failed Exception - Connection could not be allocated because:
Connection refused. Check that the hostname and port are correct and
that the postmaster is accepting TCP/IP connections. Please check the
server.log for more details.
I am not able to figure out this problem, can anyone help?
You should use the Resource Type javax.sql.DataSource. I'm not sure if this is causing the error, you may also try to change the hostname to localhost.
Can you access the PostgreSQL DB via pgAdmin or psql commands?
Cause could be that the user may not be having enough privileges on the db objects.
Grant the required privileges to the user, like:
###GRANT USAGE ON service_schema TO admin
sudo -u postgres psql -p 5432 -d service_db -c "GRANT USAGE ON SCHEMA "service_schema" TO "admin";"
###GRANT PREVILEGES ON TABLES OF service_schema TO admin
sudo -u postgres psql -p 5432 -d service_db -c "GRANT ALL ON TABLES IN SCHEMA "service_schema" TO "admin";"
###GRANT PREVILEGES ON SEQUENCES OF service_schema TO admin
sudo -u postgres psql -p 5432 -d service_db -c "GRANT ALL ON SEQUENCES IN SCHEMA "service_schema" TO "admin";"
###GRANT PREVILEGES ON FUNCTIONS OF service_schema TO admin
sudo -u postgres psql -p 5432 -d service_db -c "GRANT ALL ON FUNCTIONS IN SCHEMA "service_schema" TO "admin";"
I am trying to configure ssl certificate for PostgreSQL server. I have created a certificate file (server.crt) and key (server.key) in data directory and update the parameter SSL to "on" to enable secure connection.
I just want only the server to be authenticated with server certificates on the client side and don't require the authenticity of client at server side. I am using psql as a client to connect and execute the commands.
I am using PostgreSQL 8.4 and Linux. I tried with the below command to connect to server with SSL enabled
psql "postgresql://localhost:2345/postgres?sslmode=require"
but I am getting
psql: invalid connection option "postgresql://localhost:2345/postgres?sslmode"
What am doing wrong here? Is the way I am trying to connect to server with SSL mode enabled is correct? Is it fine to authenticate only server and not the client ?
psql below 9.2 does not accept this URL-like syntax for options.
The use of SSL can be driven by the sslmode=value option on the command line or the PGSSLMODE environment variable, but the default being prefer, SSL connections will be tried first automatically without specifying anything.
Example with a conninfo string (updated for psql 8.4)
psql "sslmode=require host=localhost dbname=test"
Read the manual page for more options.
psql --set=sslmode=require -h localhost -p 2345 -U thirunas \
-d postgres -f test_schema.ddl
Another Example for securely connecting to Azure's managed Postgres database:
psql --file=product_data.sql --host=hostname.postgres.database.azure.com --port=5432 \
--username=postgres#postgres-esprit --dbname=product_data \
--set=sslmode=verify-full --set=sslrootcert=/opt/ssl/BaltimoreCyberTrustRoot.crt.pem
Well, you could provide all the information with the following command in CLI, if a connection requires in SSL mode:
psql "sslmode=verify-ca sslrootcert=server-ca.pem sslcert=client-cert.pem sslkey=client-key.pem hostaddr=your_host port=5432 user=your_user dbname=your_db"
Found the following options useful to provide all the files for a self signed postgres instance
psql "host={hostname} sslmode=prefer sslrootcert={ca-cert.pem} sslcert={client-cert.pem} sslkey={client-key.pem} port={port} user={user} dbname={db}"
On psql client v12, I could not find option in psql client to activate sslmode=verify-full.
I ended up using environment variables :
PGSSLMODE=verify-full PGSSLROOTCERT=server-ca.pem psql -h your_host -U your_user -W -d your_db
psql "sslmode=require host=localhost port=2345 dbname=postgres" --username=some_user
According to the postgres psql documentation, only the connection parameters should go in the conninfo string(that's why in our example, --username is not inside that string)
psql -h <host> -p <port> -U <user> -d <db>
and update /var/lib/pgsql/10/data/pg_hba.conf to change the auth method to cert. Check the following link for more information:
https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html
Another pattern that worked with v8 is
psql -h host_name -p port -U user_name "dbname=db sslmode=require"