Why can't I connect to postgres from Perl? - perl

I believe i have set up Pg properly, but my script doesn't seem to be connecting to the database. I am testing with:
$database="networkem";
$user="postgres";
$password="";
$host="localhost";
$dbh = DBI->connect("DBI:Pg:dbname=$dbname;host=$host", $user, $password);
My pg_hba reads:
host all postgres 127.0.0.1 255.255.255.255 trust
I can use psql just fine via command-line and have started postmaster with -i option. What am I missing?
I also tried with another user that works fine via psql:
$user="jimbo"; $password="p2ssw0rd";
with pg_hba reading:
host all jimbo 127.0.0.1 255.255.255.255 trust

Rather than play 20 questions to debug your setup, DBI->errstr will say why the connection failed.
my $dbh = DBI->connect(...) or die DBI->errstr;
Though if I had to guess... since Postgres authenticates based on host and login user, I suspect the confusion lies between the user name you're giving to the Postgres connection and the Unix user you're logged in as.

Besides Schwern's excellent response, you can also check PostgreSQL log which, depending on the options selected in postgresql.conf may tell you a lot about what was wrong.

It is recommended that you use the 'listen_addresses' configuration option in your postgresql.conf instead of '-i' on the command line. For example:
listen_addresses = '*'
Try executing the following command as the same user you are running your perl script with:
psql -U postgres -h localhost networkem
The '-h localhost' forces a network connection instead of a Unix socket connection. If that command works, your perl script should also work.

I had the same issue. The hint above for trying "-h localhost" confirmed that I had a problem connecting over the network.
Adding the following to pg_hba.conf fixed the problem.
host all postgres 127.0.0.1/32 trust

DBI may be connecting to either the IPv4 or IPv6 postgresql server interface, depending on how things are configured.
So you may need both of these lines in pg_hba.conf:
IPv4:
host all <user> 127.0.0.1/32 trust
IPv6:
host all <user> ::1/128 trust

Related

Can't override forgettin password for Postgresql on Mac

I am having an issue where I forgot the password to the postgres shell when trying to login as postgres. I looked online and found several different things to do, but none of it worked. I have messed with the pg_hba.conf file but even with that change, it is not working. Here is what my pg_hba.conf file looks like.
# Put your actual configuration here
# ----------------------------------
#
# If you want to allow non-local connections, you need to add more
# "host" records. In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.
# CAUTION: Configuring the system for local "trust" authentication
# allows any local user to connect as any PostgreSQL user, including
# the database superuser. If you do not trust all your local users,
# use another authentication method.
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
and then i ran the follwoing command:
brew services restart postgres
and I keep getting the following error:
omars-MacBook-Pro:postgres omarjandali$ psql -U postgres Password for user postgres: psql: error: could not connect to server: FATAL: password authentication failed for user "postgres"
Anyone know how to fix this issue.
It looks like your command psql -U postgres is attempting to make a socket connection, however you've only got trust authentication set up for TCP/IP connections on localhost.
Try psql -h localhost -U postgres.
Alternatively, add a line to pg_hba.conf setting up trust authentication for socket connections:
host all all trust
and restart PostgreSQL (though you don't actually need to restart it, pg_ctl -D /path/to/data-directory reload should work if you know the actual data directory path).

Ubuntu nagios postgresql

I'm a french student and I have a little problem.
So I'm using Nagios with check_postgres plugin (here : https://exchange.nagios.org/directory/Plugins/Databases/PostgresQL/check_postgres/details)
So I'm using this plugin with command like this :
check_postgres.pl -u postgres -db bddprojet --action database_size -w $1000000 -c 10000000
It only works if I'm logged as postgres in ubuntu (su postgres), so no problem.
But when I want to add the plugin to Nagios, I have an error message:
ERROR: FATAL: Peer authentication failed for user "postgres"
I have exactly the same error message if I'm logged as 'root' on ubuntu when I use the command above.
I already check my pg_hba.conf:
# TYPE DATABASE USER ADDRESS METHOD
host all all 10.0.2.15/32 trust
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
local all postgres trust
# IPv6 local connections:
host all all ::1/128 md5
I really need help, thanks in advance. Sorry for my poor english
the line local all postgres trust makes sure that access to all databases by the postgresql user postgres are allowed without password. By default this is "peer" instead of "trust" which then only allows access with the postgresql ubuntu user. Did you restart the postgresql server after making this change ?
The postgresql server needs to be restarted after a pg_hba.conf change.
You'll have to move the line
local all postgres trust
up above the other local line and reload the server.
Lines in pg_hba.conf are processed top to bottom, and the first matching line is used.
The error message shows that you forgot to reload the server after editing the file.

psql cant connect to PostgreSQL server (postmaster) on IP and port 5432?

Please read before replying it as duplicate (as it perhaps can happen). I am running my postmaster (postgres) server. See below for 'sudo netstat -anp|grep 5432' output?
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 29606/postmaster
unix 2 [ ACC ] STREAM LISTENING 1650581 29606/postmaster /var/run/postgresql/.s.PGSQL.5432
unix 2 [ ACC ] STREAM LISTENING 1650582 29606/postmaster /tmp/.s.PGSQL.5432
I am able to connect from localhost using
psql -h localhost (OR 127.0.0.1) -d <DB> -U user -W
But when I try to connect from other hosts using tcp, by specifying
psql -h ip_add_postmaster -d <DB> -U user -W
It throws:
psql: could not connect to server: Connection refused
Is the server running on host XXXXXX and accepting TCP/IP connections on port 5432?
What's wrong here?
pg_hba.conf
# 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 md5
# IPv6 local connections:
host all all ::1/128 md5
In postgresql.conf,
listen_addresses = 'localhost, 127.0.0.1, ip_add_postmaster'
Note: ip_add_postmaster is same as my Elastic IP and not public DNS. If this information
matters.
What am I doing wrong here? Machine is hosted on Amazon EC2 and have open the port 5432.
As your netstat output indicates, it's listening at 127.0.0.1:5432 which is localhost. That is only connectable from localhost ;)
Set listen_addresses='*' in your config and it will work.
[edit]
Other things to check:
is the amazon firewall blocking anything?
is iptables blocking anything?
But first make sure the listening address is correct, your netstat output shows that it won't work like this.
listen_addresses='localhost, private_ip' fixed the issue. I was not able to start postmaster server on elastic IPs. Once postgres server started o localhost and private IPs, I was able to connect.
One other issue I have found was if you end up with two Postgres installations, the second one can choose non-default port (in my case it was 5433 i/o 5432). So checking the port in postgresql.conf might be a good idea.
I ran into this issue and tried all sorts of fixes I found across SO, and want to add a simple solution that worked for me after realizing it had to do with permissions in my case.
Simply, if you're running a psql server on Windows, you are initially restricted to the default postgres superuser for logging in, launching the server, and so on.
So, first try running from the command line:
psql -U postgres -h localhost -p 5432
and enter your password at the prompt. If you've managed to login and the server is up, then it was a permissions issues. From here, you can create a role for yourself that has login privileges to whatever database you are trying to run.
If the error persists, then consider checking postgresql.conf as mentioned above, to make sure default IP is set to * or localhost, and the port set to 5432 or whatever port you want as default.
I also ran into the same issue. On debugging, it was nothing related to the port, but due to some missing directories in the Postgres folder.
While updating Mac OS (from 10.13.1 -> 10.13.13), some folders in the directory /usr/local/var/postgres/ gets deleted. The fix was the adding the missing directories:
mkdir /usr/local/var/postgres/pg_tblspc
mkdir /usr/local/var/postgres/pg_twophase
mkdir /usr/local/var/postgres/pg_stat
mkdir /usr/local/var/postgres/pg_stat_tmp
mkdir /usr/local/var/postgres/pg_replslot
mkdir /usr/local/var/postgres/pg_snapshots
mkdir /usr/local/var/postgres/pg_logical/{snapshots,mappings}

pgadmin3: FATAL: Ident authentification failed for user "postgres"

I'm trying to register new server in pgadmin3 with following settings:
Name: postgres
Host: localhost
Username: postgres
Password: <password which works for psql>
Service: empty or postgres
But it shows error:
FATAL: Ident authentification failed for user "postgres"
I've restarted postgresql service, but to no avail.
Contents of /var/lib/pgsql/data/pg_hba.conf:
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 ident
EDIT: Tools -> Server Configuration -> pg_hba.conf is greyed out.
It looks like PgAdmin-III is probably connecting over IPv6 by default, so it's using the ident line that matches the IPv6 address for localhost, ::1/128.
If you want to use password authentication, you probably want:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
I'm not sure why you have the unix domain socket line set to trust, but that's probably OK if it's just a development machine, so leave it unchanged. It's really much safer to have it as ident (if you want the unix user to have to be the same as the Pg user) or md5 (for password auth on local unix sockets) though.
You'll need to edit pg_hba.conf directly in a text editor if PgAdmin-III doesn't have permissions to edit it. You could run PgAdmin-III as user postgres via sudo, but it's way safer (and probably easier) to just use nano or a similar command-line text editor to modify pg_hba.conf.
The password works for psql because psql will, unless told otherwise, connect over a unix domain socket, and you have that set to trust. You'll probably find you could give any password to psql and it'll still work, because it's never being asked to actually give the password, it's just being automatically trusted.
Yes this type of error is seen by every newbie user to pgadmin.
I have found this solution and it worked for me.
sudo -u postgres psql
This will ask for your system password and then you will get the postgres prompt.
and then in psql type below command to change the password.
\password
now enter the new password and re-enter it.
PostGreSQL Account Debugging Steps (Linux Specific):
Make sure you actually have it installed (not just the client, the server too).
Make sure it is running.
Make sure you know where this is - usually in /var/lib/pgsql/data - however this could be anywhere - /var/lib/pgsql/unrelated-instance. Check your postgres process to see which directory (-D argument) this is.
Modify the pg_hba.conf file in the directory from the last step. I have no idea why this step isn't in the postgres documentation.
The specific configuration has been covered in e.g. Jay and Craig Ringer's answer. Make sure to configure both IPV4 and IPV6.
Restart the server.
Test that your configuration worked. Repeat 5-7 until you can login successfully.
Important Don't stop! Now you should configure a more secure password option - postgres may be fine for doing quick local setup, but you want to be using a more secure, configurable authentication mechanism, like LDAP, Kerberos, or GSSAPI. Additionally, you want to make sure you have SSL turned on.

no pg_hba.conf entry for host

I get following error when I try to connect using DBI
DBI connect('database=chaosLRdb;host=192.168.0.1;port=5433','postgres',...)
failed: FATAL: no pg_hba.conf entry for host "192.168.0.1", user "postgres", database "chaosLRdb", SSL off
Here is my pg_hba.conf file:
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
host all postgres 127.0.0.1/32 trust
host all postgres 192.168.0.1/32 trust
host all all 192.168.0.1/32 trust
host all all 192.168.0.1/128 trust
host all all 192.168.0.1/32 md5
host chaosLRdb postgres 192.168.0.1/32 md5
local all all 192.168.0.1/32 trust
My perl code is
#!/usr/bin/perl-w
use DBI;
use FileHandle;
print "Start connecting to the DB...\n";
#ary = DBI->available_drivers(true);
%drivers = DBI->installed_drivers();
my $dbh = DBI->connect("DBI:PgPP:database=chaosLRdb;host=192.168.0.1;port=5433", "postgres", "chaos123");
May I know what i miss here?
If you can change this line:
host all all 192.168.0.1/32 md5
With this:
host all all all md5
You can see if this solves the problem.
But another consideration is your postgresql port(5432) is very open to password attacks with hackers (maybe they can brute force the password). You can change your postgresql port 5432 to '33333' or another value, so they can't know this configuration.
In your pg_hba.conf file, I see some incorrect and confusing lines:
# fine, this allows all dbs, all users, to be trusted from 192.168.0.1/32
# not recommend because of the lax permissions
host all all 192.168.0.1/32 trust
# wrong, /128 is an invalid netmask for ipv4, this line should be removed
host all all 192.168.0.1/128 trust
# this conflicts with the first line
# it says that that the password should be md5 and not plaintext
# I think the first line should be removed
host all all 192.168.0.1/32 md5
# this is fine except is it unnecessary because of the previous line
# which allows any user and any database to connect with md5 password
host chaosLRdb postgres 192.168.0.1/32 md5
# wrong, on local lines, an IP cannot be specified
# remove the 4th column
local all all 192.168.0.1/32 trust
I suspect that if you md5'd the password, this might work if you trim the lines. To get the md5 you can use perl or the following shell script:
echo -n 'chaos123' | md5sum
> d6766c33ba6cf0bb249b37151b068f10 -
So then your connect line would like something like:
my $dbh = DBI->connect("DBI:PgPP:database=chaosLRdb;host=192.168.0.1;port=5433",
"chaosuser", "d6766c33ba6cf0bb249b37151b068f10");
For more information, here's the documentation of postgres 8.X's pg_hba.conf file.
Your postgres server configuration seems correct
host all all 127.0.0.1/32 md5
host all all 192.168.0.1/32 trust
That should grant access from the client to the postgres server. So that leads me to believe the username / password is whats failing.
Test this by creating a specific user for that database
createuser -a -d -W -U postgres chaosuser
Then adjust your perl script to use the newly created user
my $dbh = DBI->connect("DBI:PgPP:database=chaosLRdb;host=192.168.0.1;port=5433", "chaosuser", "chaos123");
To resolve this problem, you can try this.
first, you have found out your pg_hba.conf by:
cd /etc/postgresql/9.5/main from your root directory
and open file using
sudo nano pg_hba.conf
then add this line:
local all all md5
to your pg_hba.conf and then restart by using the command:
sudo service postgresql restart
Add the following in line in pg_hba.conf
hostnossl all all 0.0.0.0/0 trust
And then restart the Service.
I faced the same issue. My db was on cloud
Error:
ERROR: no pg_hba.conf entry for host ".......", user ".........", database "....", SSL off
I add this configuration to resolve this,
"dialect": "postgres",
"dialectOptions": {
"ssl": {
"require": true,
"rejectUnauthorized": false
}
}
SSL parameter is the key telling DB to always use SSL for making connections.
To resolved this problem, you can try this.
first you have find out your pg_hba.conf and write :
local all all md5
after that restart pg server:
postgresql restart
or
sudo /etc/init.d/postgresql restart
If you are getting an error like the one below:
OperationalError: FATAL: no pg_hba.conf entry for host "your ipv6",
user "username", database "postgres", SSL off
then add an entry like the following, with your mac address.
host all all [your ipv6]/128 md5
If you are getting this error using node and pg module you can set ssl to not reject unauthorized access like this
const pool = new Pool({
connectionString: "your connection string",
ssl: {
rejectUnauthorized: false
}
})
For those who have the similar problem trying to connect to local db and trying like
con = psycopg2.connect(database="my_db", user="my_name", password="admin"), try to pass the additional parameter, so the following saved me a day:
con = psycopg2.connect(database="my_db", user="my_name", password="admin", host="localhost")
For those who are getting this error in DBeaver the solution was found here at line:
#lcustodio on the SSL page, set SSL mode: require and either leave the SSL Factory blank or use the org.postgresql.ssl.NonValidatingFactory
Under Network -> SSL tab I checked the Use SLL checkbox and set Advance -> SSL Mode = require and it now works.
also check the PGHOST variable:
ECHO $PGHOST
to see if it matches the local machine name
BTW, in my case it was that I needed to specify the user/pwd in the url, not as independent properties, they were ignored and my OS user was used to connect
My config is in a WebSphere 8.5.5 server.xml file
<dataSource
jndiName="jdbc/tableauPostgreSQL"
type="javax.sql.ConnectionPoolDataSource">
<jdbcDriver
javax.sql.ConnectionPoolDataSource="org.postgresql.ds.PGConnectionPoolDataSource"
javax.sql.DataSource="org.postgresql.ds.PGPoolingDataSource"
libraryRef="PostgreSqlJdbcLib"/>
<properties
url="jdbc:postgresql://server:port/mydb?user=fred&password=secret"/>
</dataSource>
This would not work and was getting the error:
<properties
user="fred"
password="secret"
url="jdbc:postgresql://server:port/mydb"/>
Please add the following line in /etc/postgresql/14/main/pg_hba.conf file
#IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
host all all all md5
while making the connection include ssl prop in configugration like this:
ssl: {
rejectUnauthorized: false
}
I've got the same issue in Azure Data Factory while connecting to Azure Database for PostgreSQL.
28000: no pg_hba.conf entry for host "", user "", database "postgres", SSL off
Here the issue was due to PostgreSQL database has ssl_min_protocol_version set to TLSV1.2 expecting a encrypted connection and the client connection was not using any encryption.
I've resolved the issue by setting the property "Encryption method" to SSL
I have used Docker container to run postgres. I faced this issue and to resolve this, I used POSTGRES_HOST_AUTH_METHOD=trust env variable at the time of starting container.
Here is an example command
docker run --name postgres --net appnet -e POSTGRES_PASSWORD=password -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres
If you want to connect postgres container to any other application which is running inside a docker container, it is best to run both the containers in same network. In this case, I have created a network appnet using docker network create command, used it to create my application container and postgres container.
Use SSL in the connection. This solves it instantly
Verify the postgres connection hostname/address in pgadmin and use the same in your connection parameter.
DBI connect('database=chaosLRdb;host="keep what is mentioned" ;port=5433','postgres',...)