Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password authentication fails when trying to connect to postgresql from clojure with jdbc

I'm learning web development in Clojure and I'm not at all an expert in either PostgreSQL or JDBC.

I'm going through the "Web development with Clojure" book and I'm stuck because I cannot connect to a PostgreSQL database, although I believe I have followed the tutorial step by step.

I connected to psql with

sudo -u postgres psql

Then I created an admin user with password 'admin'

postgres=# CREATE USER admin WITH PASSWORD 'admin';
CREATE ROLE

Then I created a database named reporting :

postgres=# CREATE DATABASE reporting OWNER admin;
CREATE DATABASE

No complaints so far.

Then from my clojure code, I attempt to connect to the database and create a table :

(ns reporting-example.models.db
  (:require [clojure.java.jdbc :as sql]))

;defining the db connection
(def db 
  {:subprotocol "postgresql"
   :subname "//localhost/reporting"
   :user "admin"
   :password "admin"})

;function for creating an 'employee' table
(defn create-employee-table []
  (sql/create-table :employee
                    [:name "varchar(50)"]
                    [:occupation "varchar(50)"]
                    [:place "varchar(50)"]
                    [:country "varchar(50)"])  
  )

; then trying to actually create the table, here's the part that breaks with an exception :
(sql/with-connection db
    (create-employee-table))

And I'm getting an ugly :

org.postgresql.util.PSQLException: FATAL: password authentication failed for user "admin"

Which does not make sense to me, the credentials seem fine.

I tried the above code from both the Counterclockwise REPL and the Leiningen REPL. I'm on Ubuntu 13.10, if that matters.

Could someone explain to me what I am doing wrong here? Thanks in advance!

like image 572
Valentin Waeselynck Avatar asked Apr 06 '14 19:04

Valentin Waeselynck


People also ask

How do I fix Postgres password authentication failed?

Restart the PostgreSQL service from the Services control panel ( start->run->services. msc ) Connect using psql or pgAdmin4 or whatever you prefer. Run ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'

How do I enable Postgres authentication?

To authenticate network connections from the PostgreSQL server's machine (non-socket connections) using passwords, you need to match a host connection type instead of local . You can then limit the acceptable addresses to the local loopback devices and allow users to authenticate using md5 or scram-sha-256 .


2 Answers

Since you can't connect with psql using the same settings there are really three possibilities:

  • wrong/typo'd password
  • wrong/typo'd username
  • you are not connecting to the same server as you created the account on.

To check the latter, connect however you did to create the account and then run

show port;

Make sure it is the same as what you specify in your app. That isn't perfect - two pg instances can share a port if one isn't listening on TCP/IP and has a different socket directory - but it is a good first test.

You should also examine the PostgreSQL server error logs, as they may contain more detailed information about the nature of the authentication failure.

like image 99
Craig Ringer Avatar answered Oct 01 '22 19:10

Craig Ringer


If you have not tried this already, review your pg_hba.conf file. It will be named something like

  • /etc/postgresql/9.6/main/pg_hba.conf (Ubuntu 16.04)
  • /var/lib/pgsql/9.3/data/pg_hba.conf (Fedora 20);

You may have to use find / -name pg_hba.conf to locate it.

At the bottom of the file, change the METHOD values to trust for local testing (see postgres docs for full information). Restart postgres to ensure everything is started clean and the new params are read:

> sudo systemctl restart postgresql     # ubuntu

Hopefully this will cure your woes. It solved my problems on Ubuntu/Fedora.

like image 27
Alan Thompson Avatar answered Oct 01 '22 18:10

Alan Thompson