Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I solve Postgresql SCRAM authentifcation problem?

I am getting an error after moving the project to production. The error is as follows while running with production server

pg_connect(): Unable to connect to PostgreSQL server: SCRAM authentication requires libpq version 10 or above.

Here is my postgreSQL version:

Development Version :

PostgreSQL 11.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit

Production Version :

PostgreSQL 11.5 (EnterpriseDB Advanced Server 11.5.12) on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit

like image 938
Alimin Avatar asked Jul 09 '20 05:07

Alimin


People also ask

How do I authenticate in PostgreSQL?

PostgreSQL supports GSSAPI with Kerberos authentication according to RFC 1964. GSSAPI provides automatic authentication (single sign-on) for systems that support it. The authentication itself is secure, but the data sent over the database connection will be sent unencrypted unless SSL is used.

What is scram Postgres?

The method scram-sha-256 performs SCRAM-SHA-256 authentication, as described in RFC 7677. It is a challenge-response scheme that prevents password sniffing on untrusted connections and supports storing passwords on the server in a cryptographically hashed form that is thought to be secure.

How can I get scram Sha 256 password?

Also, in case your local postgres db still uses/generates md5 you have to change that to scram by using following query, ran as postgres superuser: ALTER SYSTEM SET password_encryption = 'scram-sha-256'; SELECT pg_reload_conf();


6 Answers

Your application uses an API that is linked with the PostgreSQL client C library libpq.

The version of that library must be 9.6 or older, and SCRAM authentication was introduced in v10.

Upgrade libpq on the application end and try again.

If you don't need scram-sha-256 authentication, you can revert to md5:

  • set password_encryption = md5 in postgresql.conf
  • change the authentication method to md5 in pg_hba.conf
  • reload PostgreSQL
  • change the password of the user to get an MD5 encrypted password
like image 175
Laurenz Albe Avatar answered Oct 23 '22 15:10

Laurenz Albe


For those on M1-Based macs who are currently seeing this issue in docker - there appears to a bug upstream in libpg that's building against the wrong library version on ARM.

Until it's fixed, a workaround is to (at a performance hit) is to just run it via rosetta.

export DOCKER_DEFAULT_PLATFORM=linux/amd64, and re-build your images.

You'll get the latest version of libpq, and things should Just Work.

Ref: https://github.com/psycopg/psycopg2/issues/1360

like image 39
skoczen Avatar answered Oct 23 '22 14:10

skoczen


I ran into this while running a python:3.9 docker image where I had installed psycopg2-binary==2.9.3. Installing psycopg2==2.9.3 instead resolved it for me.

like image 23
ihm Avatar answered Oct 23 '22 14:10

ihm


I used to get an error SCRAM authentication requires libpq version 10 or above when running php artisan migrate in laravel 8. Then I fixed it as follows: Change authentication from scram-sha-256 to md5, then reset your password and restart the postgresql-x64-13 service and here are step by step:

  1. Step 1: Find file postgresql.conf in C:\Program Files\PostgreSQL\13\data then set password_encryption = md5
  2. Step 2: Find file pg_hba.conf in C:\Program Files\PostgreSQL\13\data then change all METHOD to md5
  3. Step 3: Open command line (cmd,cmder,git bash...) and run psql -U postgres then enter your password when installed postgres sql
  4. Step 4: Then change your password by run ALTER USER postgres WITH PASSWORD 'new-password' in command line
  5. Final: Restart service postgresql-x64-13 in your Service.
like image 15
changtraithang10 Avatar answered Oct 23 '22 15:10

changtraithang10


For Amazone linux users:

$ sudo yum install -y amazon-linux-extras

then re install the postgres client again

$ sudo amazon-linux-extras install postgresql10

Then the main part install the python package in my case it was (psycopg2) reinstall it

$ pip3 install --force-reinstall psycopg2==2.93

specification: python3.8.9

like image 7
zabusa Avatar answered Oct 23 '22 14:10

zabusa


If you want to keep the scram-sha-256 for security. You need to update your client postgreSQL libraries, due to php-pgsql default version does not support it.

CentOS/Rhel/Rocky

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install postgresql13

This will update the server/client libpq in order to keep using scram-sha-256

like image 3
Andrés Julián Avatar answered Oct 23 '22 14:10

Andrés Julián