How to connect to an alternative local postgresql cluster for the fist time? - postgresql

In Ubuntu 16.04 I created second postgres database cluster, called cmg, with a local user as the admin user:
pg_create -u "local_username" -g "local_usergroup" -d /path/to/data/dir 9.5 cmg
The cluster was started with:
pg_ctrlcluster 9.5 cmg start
which ran successfully (pg_lsclusters show both are online)
The problem is I cannot connect to the cluster using psql as is normally done.
I tried using:
psql -h 127.0.0.1 -w -p5433 -U local_username
which fails with:
psql: fe_sendauth: no password supplied"
Is there any way to connect to the specific cluster?

use psql -h your_socket_dir -p5433 -U postgres to connect locally (uses peer auth by default - thus high chahce to login wothout password)
once logged in - set up password (create user if needed) and use it connecting remotely
psql -h 127.0.0.1 -p5433 -U local_username
in your connect string you had -w which is never ask for a password https://www.postgresql.org/docs/current/static/app-psql.html which would by default work only for local connections

I think the default pg_hba.conf when you start up a new cluster expects you to authenticate with peer connections, so you need to change user to your local user before connecting
[root#server~]# su - local_username
>> Enter password:
> password
[local_username#server~]# psql -h 127.0.0.1 -p 5433
You can check your pg_hba.conf file in /path/to/data/dir/pg_hba.conf to see how it expects you to authenticate.
Alternatively, if you cannot get access as your 'local_username' then instead su to postgres user in the instructions above and it should work

Related

pgAdmin and terminal: FATAL: password authentication failed for user

sup guys!
I'm trying to create a server local in pdAdmin 4, but everytime i'm trying to create i get this error:
[Error in pgAdmin]
in case 'veterano' its my username...
my tries to make this run (but doesnt work)
Checking if Postgres Service is Installed correctly:
$ sudo systemctl is-active postgresql
terminal back:
active
$ sudo systemctl is-enabled postgresql
terminal back:
enabled
$ sudo systemctl status postgresql
terminal back:
active (exited)
$ sudo pg_isready
terminal back:
/var/run/postgresql:5433 - accepting connections
My configuration to pg_hba.conf :
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
(every time after change something in pg_hba.conf I run $ sudo systemctl restart postgresql to restart postgress service.)
Checking psql and the new superusers:
$ sudo -u postgres createuser -s $USER
terminal back
createuser: error: creation of new role failed: ERROR: role "veterano" already exists
if I try
psql -U veterano i can login... so i try \du to check list of roles
terminal back
List of roles
Role name | Attributes | Member of
-------------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
veterano | Superuser, Create role, Create DB | {}
So I try create a localhost server in terminal:
$ psql -h localhost -d mydatabase -U veterano -p 5432
terminal back:
Password for user veterano:
(I put my password maked with ALTER USER veterano PASSWORD 'newPassword';)
terminal back error
psql: error: FATAL: password authentication failed for user "veterano"
I really don't know what to do... I tried everything and still cant create a local server in pgAdmin 4 or directly in terminal.
Using Ubuntu 20.04.2 LTS
(Sorry for my english )
This is probably too late. but I have the same issue with veterano, and what I did was to change the password for the account "postgres" that I have :
(For those who don't know, postgresql has a user named "postgres" by default after installation)
Steps:
Access PSQL
$ sudo su - postgres
Then try to change the password for "postgres" user by typing :
postgres=# \password
You can then specify any password you want for this account.
Once thats done, you can use pgAdmin to connect using "postgres" username and the password that you just set.
Either you didn't set a password for the user, or you set a different password.
Connect using psql with psql -U veterano and set the password:
veterano=> \password
Enter new password:
Enter it again:
Now you should be good.
Try putting quotation marks '' on your password at your config file.
First we have to get inside postgres through using
sudo -u postgres psql postgres
Enter password for user postgres
Now the terminal will be like this:
postgres=#
Now enter the given line after postgres=#
CREATE USER username WITH PASSWORD 'your password';
(put your password inside quotes('') and don't forget semicolon(;)

pg_dump: How to set username and password for the *.sql output file?

I want to create a local copy of an external postgres database. I am asked for the password of the local copy. Where can I set/access this password? I never set a password.
pg_dump -h external_hostname -p 5432 -U postgres db > db_copy.sql
createdb -p 5432 -h 127.0.0.1 -U postgres db_copy
I get the error
createdb: could not connect to database template1: FATAL: password authentication failed for user “postgres”
First you should find your pg_hba.conf file with
show hba_file;
Its place can be change but if you use debian based system, must be about etc/postgresql/9.6/main/pg_hba.conf
open this file and change
local all postgres peer
to
local all postgres trust
After all you should reload postgresql service from root, or easy way to reload is that, you run this select query by open psql :
select pg_reload_conf();

Postgresql (pgsql) client expecting sockets in different location from postgrsql-server

While trying to connect to postgres running locally on my workstation, I get:
$ sudo -u postgres psql -c "create role ..."
could not change directory to "/home/esauer/workspace/cfme"
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
My postgres-server install creates sockets in /var/run/postgresql.
How do I get the client to look in the proper location?
Check the --host option with psql --help.
Then you can make it permanent by setting it in your .psqlrc user file.
In your case try:
psql -h /var/run/postgresql -d your_database

AWS RDS: How to Connect to Instance

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

Using psql to connect to PostgreSQL in SSL mode

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"