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