1

I can't connect to database using this code:

void connect_to_db(){ connection = PQconnectdb("user=username password=123 dbname=project_db"); printf("%s\n", PQerrorMessage(connection)); } int main() { connect_to_db(); PQfinish(connection); return 0; } 

when I run the program it prints the error: FATAL: Peer authentication failed for user "username". I had created a database and username with these commands in psql shell:

CREATE DATABASE yourdbname; CREATE USER youruser WITH ENCRYPTED PASSWORD 'yourpass'; GRANT ALL PRIVILEGES ON DATABASE yourdbname TO youruser; 

this is pg_hba.conf file:

# 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 ident host all all 127.0.0.1/32 md5 # IPv6 local connections: #host all all ::1/128 ident 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 ident #host replication all ::1/128 ident 
6
  • @SamiKuhmonen where is this address in fedora?/etc/postgresql/9.1/main/pg_hba.conf* Commented Jun 25, 2020 at 21:45
  • 3
    need actual pg_hba.conf file, not sample. Commented Jun 25, 2020 at 22:43
  • 1
    pg_hba.conf.sample itself is not the real pg_hba.conf. It is a template used by initdb to create the real one. Commented Jun 25, 2020 at 22:48
  • 1
    'locate' takes some time for it's index to pick up new files. Did you just init the database recently? Can you find postgresql.conf? It should be in the same directory as that, but it might also be given an alternate name or location. Commented Jun 25, 2020 at 23:06
  • 2
    Change 'peer' to 'md5' and restart the database. Commented Jun 26, 2020 at 1:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.