Can't connect to postgresql "Role does not exist" - postgresql

I'm trying to connect to postgresql from a docker container but I get following error:
2019-03-02 20:10:45.218 MSK [2777] spectrum_user#spectrum_db FATAL: password authentication failed for user "spectrum_user"
2019-03-02 20:10:45.218 MSK [2777] spectrum_user#spectrum_db DETAIL: Role "spectrum_user" does not exist.
Connection matched pg_hba.conf line 100: "host all all 172.17.0.0/16 md5"
Looks like it successfully connects but fails to authenticate. And I don't have idea why. I can connect to db with psql. This role definitely exists.
I'm using Ubuntu 18.04 LTS
Does anyone have suggestions?
UPDATE: It appeared that I had two versions of postgresql running simultaneously and I tried to connect to wrong postgresql instance.

It appeared that I had two versions of postgresql running simultaneously and I tried to connect to wrong postgresql instance.

You'll need to edit the pg_hba.conf.
On my install of Ubuntu 18.04 this is the path:
/etc/postgresql/10/main/pg_hba.conf
You can try:
sudo vim /etc/postgresql/10/main/pg_hba.conf
replace vim with your choice of text editor. If a file doesn't open with this command (a file that already has settings in it) then you'll need to explore around /etc/postgresql to find the pg_hba.conf file. Try:
find -name pg_hba.conf
Once you find and open the file, edit the following lines:
Under the comment # IPv4 local connections:
host all all 172.17.0.1/24 trust
Now you'll need to edit one more file:
sudo vim /etc/postgresql/10/main/postgresql.conf
Change any reference of listen_addresses to:
listen_addresses = '*'
After you've saved all the above changes, run this command:
sudo service postgresql restart
Note: this configuration isn't secure for a production environment, and you would need to configure your firewall and change some of these settings to make it secure. This is for development only.
Now, as long as you're pointing at your host OS's IP address for your postgres URL and the user exists on the system and the role exists in the database, you'll be able to connect to the host database from the docker container. I also always bind an HTTP port, so this may come into play. I'd need to see your docker file to give any information on that.
You also need to allow the port on your firewall:
sudo ufw allow from 172.17.0.1/24 to any port 5432
If you don't care about security and the above still doesn't work, just open port 5432. Be aware that opening a port can be unsafe.

Related

Cant access database from pgAdmin

I`m using postgresql database on my web server (ubuntu + nginx) and it works with backend app. I want to connect remotely using pgAdmin
but it always throw this error:
could not connect to server: Connection timed out(0x0000274C/10060)
Is the server running on host "ip" and accepting
TCP/IP connections on port 5432?
I already add to my pg_hba.conf this line:
host all all all password
and also add to postgresql.conf line:
listen_addresses = '*'
netstat -nlt does next result, and it`s enough in tutuorials i found, but still cant access my database
Found a solution, it works as well after this command:
sudo ufw allow 5432/tcp
Open file named pg_hba.conf
sudo vi pg_hba.conf
and add this line to that file
host all all 0.0.0.0/0 md5
It allows access to all databases for all users with an encrypted password
restart your server
sudo /etc/init.d/postgresql restart

Disable authentication in postgres entirely

I've installed postgres 10 under Ubuntu, and trying to perform an import from the command line like:
psql -f dump.sql -U postgres -d dbname
The operation fails with the message:
psql: FATAL: Peer authentication failed for user "postgres"
I read I should edit the pg_hba.conf file. The problem starts here, because I don't have any pg_hba.conf file on my system. The only similar file I have found is /usr/share/postgresql/10/pg_hba.conf.sample but it doesn't contain any code, just comments.
So what I did was create a pg_hba.conf in this same directory with the contents:
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
I also put the same into pg_hba.conf.sample (although I don't think this file is used anyhow but anyways..)
Then, I restarted the server with:
sudo service postgresql restart
Nothing changed, I'm getting the same error.
Then, I tried all possible combinations in pg_hba.conf, from 'trust' to 'md5', 'localhost', 127.0.0.1, anything I could find on the internet. Even created new users and tried with those, also installed pgadmin to grant all access to all users to every database.... Restarted the OS, reinstalled postgres, tried everything again and again..
The problem still persist.
Anything else I could try? Is there a way to disable the authentication entirely?
It's meant to be a database for local testing purposes only, and I've been trying for three hours just to get it to work.

Unable to connect to PostgreSQL on remote server via Pgadmin 4

I installed Ubuntu 14.04 on Azure recently. Have been trying to connect to PostgreSQL but the server refuses the connection. I checked and confirmed that it was online. I also tried changing the settings to trust on pg_hba.conf and I also edited the Postgresql.conf file to listen to all addresses. Furthermore, I checked my firewall settings on Windows and allowed Pgadmin 4 to go through. Despite following all the instructions in this question (Unable to connect PostgreSQL to remote database using pgAdmin), I was unable to connect. What should I do?
I once had such issue with pgAdmin4 on win 10. Here is the step I took to connect to my remote server
first enable port 5432 to pass through firewall in ubuntu:
sudo ufw allow 5432/tcp
Then edit your postgresql.conf file and add
listen_addresses = "*"
file can be found at /etc/postgresql//main/postgresql.conf
Proceed to edit pg_hba.conf and add
host all all 0.0.0.0/0 md5
Now stop the server using/etc/init.d/postgresql stop and restart /etc/init.d/postgresql start
You should be able to connect now. However, you can allow pgAdmin4 to pass through the windows firewall
control panel > System and Security > Allow an app through windows firewall
You can also allow same app for any antivirus you've installed
Note:
If you still cannot connect, you can reset your postgres user's password NOT linux default user
sudo -u postgres psql postgres
# \password postgres
Enter new password
Then use this new password to connect your pgAdmin4 using
postgres as Maintenance database
postgres as username
then new password
Hopefully, you should be able to connect
Enable your postgresql server to start at boot
sudo systemctl enable postgresql
Start your postgresql server
sudo systemctl start postgresql
verify your postgresql server is running:
sudo systemctl status postgresql

"psql: could not connect to server: Connection refused" Error when connecting to remote database

I am trying to connect to a Postgres database installed in a remote server using the following command:
psql -h host_ip -U db_username -d db_name
This is the error that occurs:
psql: could not connect to server: Connection refused
Is the server running on host "<host_ip>" and accepting
TCP/IP connections on port 5432?
Postgres installed version is 9.4.
Host operating system: Ubuntu 15.04
Client operating system: Centos 7
I already tried the following but the issue remains unresolved:
Edited pg_hba.conf file to include
host all all 0.0.0.0/0 md5
Edited 'postgresql.conf' and changed the listen parameter to
listen_addresses='*'
Restarted Postgres service.
Disabled firewall and iptables on host and client.
I checked by running the psql command locally and it worked.
I tried the second solution given in this question. Running nmap gave me the following output:
Starting Nmap 6.47 ( http://nmap.org ) at 2015-09-07 18:08 IST Nmap scan report for 10.17.250.250 Host is up (0.0000040s latency). Not shown: 997 closed ports PORT STATE SERVICE 22/tcp open ssh 25/tcp open smtp 80/tcp open http
Am I missing something? Hope someone can help.
cd /etc/postgresql/9.x/main/
open file named postgresql.conf
sudo vi postgresql.conf
add this line to that file
listen_addresses = '*'
then open file named pg_hba.conf
sudo vi pg_hba.conf
and add this line to that file
host all all 0.0.0.0/0 md5
It allows access to all databases for all users with an encrypted password
restart your server
sudo /etc/init.d/postgresql restart
Check the port defined in postgresql.conf. My installation of postgres 9.4 uses port 5433 instead of 5432
I have struggled with this when trying to remotely connect to a new PostgreSQL installation on my Raspberry Pi. Here's the full breakdown of what I did to resolve this issue:
First, open the PostgreSQL configuration file and make sure that the service is going to listen outside of localhost.
sudo [editor] /etc/postgresql/[version]/main/postgresql.conf
I used nano, but you can use the editor of your choice, and while I have version 9.1 installed, that directory will be for whichever version you have installed.
Search down to the section titled 'Connections and Authentication'. The first setting should be 'listen_addresses', and might look like this:
#listen_addresses = 'localhost' # what IP address(es) to listen on;
The comments to the right give good instructions on how to change this field, and using the suggested '*' for all will work well.
Please note that this field is commented out with #. Per the comments, it will default to 'localhost', so just changing the value to '*' isn't enough, you also need to uncomment the setting by removing the leading #.
It should now look like this:
listen_addresses = '*' # what IP address(es) to listen on;
You can also check the next setting, 'port', to make sure that you're connecting correctly. 5432 is the default, and is the port that psql will try to connect to if you don't specify one.
Save and close the file, then open the Client Authentication config file, which is in the same directory:
sudo [editor] /etc/postgresql/[version]/main/pg_hba.conf
I recommend reading the file if you want to restrict access, but for basic open connections you'll jump to the bottom of the file and add a line like this:
host all all all md5
You can press tab instead of space to line the fields up with the existing columns if you like.
Personally, I instead added a row that looked like this:
host [database_name] pi 192.168.1.0/24 md5
This restricts the connection to just the one user and just the one database on the local area network subnet.
Once you've saved changes to the file you will need to restart the service to implement the changes.
sudo service postgresql restart
Now you can check to make sure that the service is openly listening on the correct port by using the following command:
sudo netstat -ltpn
If you don't run it as elevated (using sudo) it doesn't tell you the names of the processes listening on those ports.
One of the processes should be Postgres, and the Local Address should be open (0.0.0.0) and not restricted to local traffic only (127.0.0.1). If it isn't open, then you'll need to double check your config files and restart the service. You can again confirm that the service is listening on the correct port (default is 5432, but your configuration could be different).
Finally you'll be able to successfully connect from a remote computer using the command:
psql -h [server ip address] -p [port number, optional if 5432] -U [postgres user name] [database name]
Make sure the settings are applied correctly in the config file.
vim /etc/postgresql/x.x/main/postgresql.conf
Try the following to see the logs and find your problem.
tail /var/log/postgresql/postgresql-x.x-main.log
Following configuration, you need to set:
To open the port 5432 edit your /etc/postgresql/9.1/main/postgresql.conf and change
# Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
In /etc/postgresql/10/main/pg_hba.conf
# IPv4 local connections:
host all all 0.0.0.0/0 md5
Now restart your DBMS
sudo service postgresql restart
Now you can connect with
psql -h hostname(IP) -p port -U username -d database
Step 1: edit file potgresql.conf
file location should be : etc/postgresql/10/main/
Look for:
#Connection Settings -
#listen_addresses = '' # what IP address(es) to listen on;
remove # before listening addresses
add '*' :
listen_addresses = '*'
Step 2: edit file pg_hba.conf
file location should be : etc/postgresql/10/main/
add below given line at the end
host all all 0.0.0.0/0 md5
Step 3: restart postgres server
sudo /etc/init.d/postgresql restart
Step 4: check postgres server status
sudo netstat -plunt |grep postgres
Make sure you are using same port to access the DB
Mine was quite straightforward if you are on a Mac try:
brew install postgres
This will tell you if you have it already install and what version or install the latest version for you if not then run
brew upgrade postgresql
This will make sure you have the latest version installed then finally
brew services start postgresql
This will start the service again. I hope this helps someone.
I think you are using the machine-name instead of the ip of the host.
I got the same error when i tried with machine's name. Because, It is allowed only when both the client and host are under same network and they have the same Operating system installed.
In my case, I did not change azure default security policy in management portal. The original is port 22 allowed and the rest are all denied. As long as I add 5432 port, everything becomes good.
The following helped me on macos Mojave:
$sudo mv /usr/local/var/postgres /usr/local/var/postgres.save
$brew uninstall postgres
$brew install postgres
See the port and make a port change in postgresql.conf. My installation of postgres 9.4 uses port 5431 or 5434 instead of 5432.
If it say the port is in use so change the port.
And check if you give password in psql installation so give the password in file and save it.
In my case I had removed a locale and generated another locale. Database failed to open because of fatal errors in the postgresql.conf file, on 'lc_messages', 'lc_monetary', 'lc_numberic', and 'lc_time'.
Restoring the locale sorted it out for me.
Another situation,postgresql.confandpg_hba.conffile not locate at /etc/postgresql/9.1/main/.Because postgres can start at any location you set.
For example when you use command pg_ctl -D /tmp/pgsql/ start ,the postgresql.conf and pg_hba.conf will located at /tmp/pgsql/.
I had the exact same problem, with my configuration files correct. In my case the issue comes from the Eduroam wifi I used : when I connect via another wifi everything works. It seems that Eduroam blocks port 5432, at least in my university.
Try to migrate your database. For instance, if you are using Heroku to host your project and with Django, then try heroku run python manage.py migrate command; the error should go away.
I had a problem like this where I had to ssh into a server and than run a query in psql console so the query was in a script but everytime I got this error psql not found so what I did was just added the psql full path from the bin which we get from cat .bash_profile and its done
PATH=$PATH:/usr/local/pgsql/bin:/usr/local/mysql/bin
So I added the whole /usr/local/mysql/bin/psql intead of just psql for remote execution.
and another one here:
both host and remote are on real servers
you need '*' exactly.
'localhost , xxx.xxx.xxx.xxx' will not work. all these answers(i've seen two) should be wiped out.
what you don't need : host all all 0.0.0.0/0 md5 and this stuff
For me, I just removed the existing PostgreSQL 14 Server which was on the left-hand side of the pgAdmin4 GUI interface under the servers and then I manually added a new server from the option which is inside Quick Links of pgAdmin4.
I followed the documentation of bitnami.com.
I had a problem with access to external server via 5432.
I noticed that any network but mine saw the service
nmap server -p 5432
Fortunatelly, I recalled that I was playing with exposing my internal postgres server to outside world using my mikrotik router.
Somehow it effectively closed external 5432 for internal network.
As soon as I removed all nat rules with 5432 port - it worked like a charm.

Openbravo - Connecting pgadmin with postgresql database in Ubuntu

I have installed Openbravo ERP 3.0 in Ubuntu 12.04(LTS). After completed the installation I tried to connect with postgresql database using pgadmin 9.1. I gave all the details as mentioned in this link: http://wiki.openbravo.com/wiki/Installation/Appliance/Openbravo.
Also I have changed the config file settings like listen_addresses="*". I got this error after I click "Ok" button
Could anyone give suggestion to fix this error? Thanks in advance.
Actually the problem comes because of not mentioning the local cluster or data area for PostgreSQL. To do this after installing Openbravo, need to give this command in terminal
psql -d openbravo -U tad -h localhost -p 5932
Finally give the PostgreSQL configuration details as mentioned in this link
It works fine now...
To be able to reach the server remotely you have to add the following line into the file: /var/lib/pgsql/data/postgresql.conf:
listen_addresses = '*'
PostgreSQL, by default, refuses all connections it receives from any remote address. You have to relax these rules by adding this line to /var/lib/pgsql/data/pg_hba.conf:
host all all 0.0.0.0/0 md5
This is an access control rule that lets anyone login from any address if a valid password is provided (the md5 keyword). You can use your network/mask instead of 0.0.0.0/0 to only allow access from certain IP addresses.
When you have applied these modifications to your configuration files, you will need to restart the PostgreSQL server.
/etc/init.d/postgresql start
Now, you will be able to login to your server remotely