I want to run a simple PostgreSQL query with Erlang and epgsql similar to Erlang and PostgreSQL. I am getting
** exception error: no match of right hand side value
{error,
{authentication,
[{severity,'Ð\222Ð\220Ð\226Ð\235Ð\236'},
{code,"28000"},
{message,[208,191,208,190,208,187,209,140,208,183,208,190,208,178,208,176,209,130,208,181|...]},
{file,"auth.c"},
{line,302},
{routine,"auth_failed"}]}}
on
{ok, C} = pgsql:connect("localhost",
"postgres",
"password",
[{database, "postgres"}]).
My PostgreSQL instance works fine and I can add a table to my database or run simple queries. But I need to do it with epgsql. And furthermore, I can't read error message, this is what I can see:
[208,191,208,190,208,187,209,140,208,183,208,190,208,178,208,176,209,130,208,181|...]
I installed egpsql, did a direct test, and confirmed that the second parameter for connect is the username and the third is the password:
{ok, C} = pgsql:connect("localhost", "username", "password", [{database, "db_name"}]).
{ok, Columns, Rows} = pgsql:squery(C, "select * from myschema.mytable").
I have seen similar error 28000 INVALID AUTHORIZATION SPECIFICATION, while using epgsql:connect function.
I have followed the folks suggestion, changing the pg_hba.conf local method type to trust and ident. But no luck!. Tries with, by creating a new user and grant permission on the psql side as well. But no luck.
local all all password #trust
Finally, after the following below update in the pg_hba.conf file(restart of the postgresql service), it is working for me on centos 7.
host all all 127.0.0.1/32 password #ident
Related
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.
When I try to connect to the database on postgres via jdbc, I get the following error:
org.postgresql.util.PSQLException: ERROR: Unsupported startup parameter: search_path
This is how I create the connection:
val connection = DriverManager.getConnection(profile.connection + Option(profile.catalog).getOrElse("")+ "?currentSchema="+Option(profile.schema).getOrElse(""),
profile.user, profile.password)
I use scala and a custom version of postgres.
pgbauncer
In short, pgbouncer at least my version does not work with the search_path parameter, this discussion led me to this idea. There are two ways to fix this problem:
Change the pgbouncer config file by adding
IGNORE_STARTUP_PARAMETERS: search_path
Make a connection without using the currentSchema parameter in the connection string and create connection like this:
val connection =
DriverManager.getConnection(
profile.connection + Option(profile.catalog).getOrElse(""),
profile.user, profile.password)
Then he will choose the scheme according to the rule set, in search_path, they usually set something like "$user", public, in this case, when connecting, he first tries to choose the same scheme as the user name, and if he does not find such a scheme, he chooses public.
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.
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
I am making a web app in Go and PostgreSQL using two different computers. The setup is the same on both computers (Ubuntu with last versions of Go and PostgreSQL). The problem is that I cannot get my app to connect to the database on my laptop.
I use this piece of code:
func (db *Database) Dial(user string, password string, dbname string) {
var err error
db.Conn, err = sql.Open("postgres", "user="+user+" password="+password+" dbname="+dbname+" sslmode=require")
if err != nil {
fmt.Println("Connection to " + dbname + " not possible!")
log.Fatal(err)
}
err = db.Conn.Ping()
if err != nil {
fmt.Println("Ping to " + dbname + " not possible!")
fmt.Println(err)
}
}
And I get:
Ping to my_database not possible!
driver: bad connection
I found many questions on SO with the same error, but I found no solution that would solve my case.
Also, on my laptop, just like on my desktop computer, there is a user postgres and I can connect to the database through psql, so the daemon is active and the password is right. I use the exact same setup and code on both computers.
My question is: how do I get more information about the error? I find "bad connection" to be too vague. I'm sure that a bit more of information would help me a lot.
Also, do you have an idea of would could cause the error?
Update
The PostgreSQL log says this:
2014-09-29 14:23:26 EDT FATAL: password authentication failed for user "postgres"
2014-09-29 14:23:26 EDT DETAIL: Connection matched pg_hba.conf line 92: "host all all 127.0.0.1/32 md5"
But I double checked the password and it shoud be fine. I can also log in as the posgres user, run the psql command and execute queries.
I found a way to fix this.
I log in as postgres and run psql postgres. In other words, I connect to the postgres database. Then I run the command \password and enter a new password.
I entered the exact same password that I have been using since the beginning and somehow now it works.
The problem is solved, but if someone has an idea about what could have been the cause, I'm interested. Maybe it has something to do with password expiration?