Unable to connect to PostgreSQL on remote server via Pgadmin 4 - postgresql

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

Related

Troubles connecting PostgreSQL installed in WSL2 Ubuntu from the Windows 10 host

I'm having big troubles connecting to PostgreSQL, installed in WSL2 Ubuntu, from the Windows 10 host.
Here is what I have done so far.
Set password for postgres user.
Set the following in pg_hpa.conf
local all postgres md5
host all all 0.0.0.0/0 md5
Set the following in postgresql.conf
listen_addresses = '*'
Tested successfully that I can connect with psql -U postgres - so password should be set correctly.
When I try to connect using either pgAdmin or psql from the Windows 10 host, I get the following error.
error: could not connect to server: FATAL: password authentication failed for user "postgres"
Anything else I can change in pg_hpa.conf or other PostgreSQL configurations which might help?
I'm thinking that I could have something to do with that WSL2 has its own IP, even tough that connections from the hosts are made appear coming from localhost. The connection seems to be made, but authentication failed for some reason when it is not made from within Ubuntu.
I later tested with nc -l 5432 and for some reason, port 5432 didn't reach WSL2 on local host, but did on WSL-2 IP. If I use any other free port e.g. 5434 (free both on Windows and WSL-2) it works fine and I can connect to the PostgreSQL service on WSl-2 through localhost on windows.
I didn't change any options since the original posted question - only the port.
Update: It turns out that there indeed was a process from an old install of postgres on Windows which was listening on port 5432. This of cause explains it all. ..:/
As was pointed out by the original poster, ensure that you do not have a Windows service already running and listening on that port. In my case it was a PostgreSQL instance installed as a Windows service. Whatever the underlying networking support, it seems you can have a process/service listening on port 5432 in Windows as well as a separate Linux process listening on port 5432 from WSL2.
It was not enough to stop the Windows PostgreSQL service. Connecting from the Windows pgAdmin4 installation program failed. I also disabled the service from Microsoft Services console.
I needed to also stop and start the WSL2 postgresql. Only then was I successfully able to connect from my Windows pgAdmin4 installation program to the WSL2 postgreSQL

Amazon EC2 and PostgreSQL: cannot connect due to time out error

I think I followed any possible guide before asking here.
I am trying to remotely connect to a PostgreSQL db installed on a EC2 Linux instance (Ubuntu server 18.04, free tier if it matters).
Here are all the steps I did before hitting a wall and begging for help.
I connected to my instance via SSH
I downloaded and installed postgres following this docs
I created a new used (umberto)
I created a new DB (wondermap)
I created a new OS user with the same name as in step 3 (umberto)
I connected to my new db with my new user from the server and everything woked fine
I edited /etc/postgresql/10/main/pg_hba.conf adding these lines:
host all all 0.0.0.0/0 md5
host all all ::/0 md5
I edited /etc/postgresql/10/main/postgresql.conf changing #listen_addresses = 'localhost' into listen_addresses = '*'
i restarted the postgres server with sudo service postgresql restart
I added a new Inbound rule in the instance security group like so:
TCP port:5432 source:0.0.0.0/0, ::/0
After all, I am trying to connect from my local mahine (either using pgadmin3 or psql) but I still receive a timeout error (host is hidden):
psql -h <PUBLIC-IP-OF-MY-INSTANCE> -U umberto -d wondermap
or even
psql -h <PUBLIC-IP-OF-MY-INSTANCE> -U postgres
psql: could not connect to server: Connection timed out
Is the server running on host "<PUBLIC-IP-OF-MY-INSTANCE>" and accepting
Doing SELECT version(); shows:
PostgreSQL 10.7 (Ubuntu 10.7-0ubuntu0.18.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0, 64-bit
What could be the problem?
Apparently, I stil had to unblock postgres port on server's firewall.
sudo ufw allow 5432/tcp
That did the trick!

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

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.

Create new local server in pgadmin?

I have PostgreSQL 11 and PGadmin 4 installed on windows. Currently I'm connected to a AWS server which hosts all of my data.
I want to create a local server (localhost) as a testing environment where I can experiment. I can't seem to do it though, and the other similar questions on stack don't help. Here's what my process is:
in pgAdmin, right click 'Servers' and go Create>Server
On the 'Create - Server' pop up box, i type in Name: Localserver. For 'connection' I type localhost. Port I leave as default '5432', db: postgres, username: postgres password: empty
click save.
however, I get an error:
Unable to connect to server:
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
What should I do? I am the admin if that makes a difference.
As a new Postgres user, I did not understand how to make use of Postgres on Ubuntu. So I'm just going to chime in and help out other newbies who perhaps cannot figure out how to work with Postgres on Linux. If you're using Windows, steps should be similar.
Before you get to using PgAdmin, a bit of configuration is required. Most will happen in your terminal at first.
Open a terminal using Ctrl + Alt + T if you're on a PC. Or just pres ALT + F1 and begin typing Terminal.
Let's start with the basics first and make sure you have proper
installation.
1. Installing Postgres Latest
1.1 update the system software packages
sudo apt update
1.2 install latest version of PostgreSQL from default Ubuntu repositories
sudo apt install postgresql
the installer will create a new PostgreSQL collection of databases
that will be managed by a single server instance
Default data directory : /var/lib/postgresql/your-version/main
Configurations files : /etc/postgresql/your-version/main
2. Checking if Postgres Service is Installed
2.1 Check if Postgres is Active
sudo systemctl is-active postgresql
You should see : active
2.2 Check if Postgres is enabled
sudo systemctl is-enabled postgresql
You should see : enabled
2.3 Check Postgres Service status
sudo systemctl status postgresql
You should see : active (exited) marked in green
2.4 Check if Postgres is ready to accept connections
sudo pg_isready
You should see : /var/run/postgresql:5432 - accepting connections
3. Configuring Postgres Authentication
3.1 Opening the pg_hba.conf as SUPERUSER
sudo code --user-data-dir=~/root /etc/postgresql/13/main/pg_hba.conf
I'm using visual studio code so for me code is vsc codename. If you're using vim or sublime just replace code with your text editor name.
3.2 Configuring pg_hba.conf
Notes: you shouldn't need to change anything here, just make sure your
configuration files matches the following lines :
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
Hit save and close.
3.3 Restart Postgres Service
sudo systemctl restart postgresql
4. Create NEW Server
For me, this is where all my confusion was. Before you use PgAdmin,
you need to create a server in your terminal, then you can connect and
manager it with PgAdmin just like you would with PhpMyAdmin. It's
actually easier.
4.1 Access the PostgreSQL database shell
sudo su - postgres
psql
You will then see this : postgres=#
4.2 Creating new server and user
postgres=# create user bob with superuser password 'admin';
That's how you create new user and server in Postgres. Let's move on to PgAdmin.
5. Installing pgAdmin4
5.1 Add public key for the repository
curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add
Notes : if you don't have curl your Ubuntu will give you the command to install it
5.2 create the repository configuration file
sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
5.3 install pgAdmin4
sudo apt update
sudo apt install pgadmin4
5.4 run the web setup script installed with the pgadmin4 binary package
sudo /usr/pgadmin4/bin/setup-web.sh
It will asks you for your email address and password. This email and
password is required to login to the PgAdmin4 web interface
6. Accessing pgAdmin4 Web Interface
6.1 Open your favorite browser
type in the address of the PgAdmin web interface. It usually looks like this
http://127.0.0.1/pgadmin4
Note : After installation the web setup script will tell you exactly
where to access the web interface.
When you see the login screen, enter the email and password you've chosen during the web setup script.
6.2 Adding New Server Connection
6.2.1 Click on Add New Server
6.2.2 Under General tab enter a name for your server. ANY name you would like, it doesn't matter. You could enter PgServer1 or whatever. Don't change anything else.
6.2.3 Switch to Connection Tab
Hostname/ Address : localhost
Port : 5432
Maintenance database : postgres (always)
Username : **bob** (the username youve chosen at 4.2)
Password : admin (or any password you chose at 4.2)
Hit Save.
Voila! You should be connected successfully. If not, just open terminal and create a new user like we did at 4.2
Notes : to create databases is very easy just right click on your
servername
create > databases
Useful Resources & Tutorials
PostgreSQL Fundamentals : Queries and the likes
PostgreSQL & JSON : useful for dealing with JS apps
PostgreSQL & Nodejs : Create realtime apps with nodejs and socket.io
More PostgreSQL Nodejs
https://github.com/supabase/realtime
https://whatsyourssn.com/posts/real-time-app-socket-postgresql/
UPDATE 2023
While following my own tutorial I ran into certificate issues at step 5 when I tried to create the repository file. The full error I was getting was this.
I want to post the solution that worked for me here.
This is probably one of two things.
You have set up a Postgres server, but have not adjusted listen_addresses in your postgresql.conf file, or
You have not set up a Postgres server on your local machine. ("Create Server" is a bit misleading, it should probably be "Create Server Connection".)
Usually a “connection refused” error indicates that the database server is either 1) not running, or 2) configured in such a way that it is not listening to the right port or IP address. Be sure to check ps -ef to see if Postgres is running, and also look at postgresql.conf to see if port and listen_addresses are set properly.
make sure the postgres service is running.
example in Linux : systemctl enable postgresql.service
Why? Attempting to connect to the server that is not running.
Action: Start your PostgreSQL server.
The path in the command below points to the data directory configured during the installation of PostgreSQL. Run this in Windows cmd.
pg_ctl -D "C:\user\PostgreSQL\data" start
>server started
After this go to pgAdmin and follow the initial steps as in question.
Create Server
Under General: Name: mytestServer
Under Connection: Hostname: localhost, Password: yourPassword,
keep other settings as default
Hit save
If you haven't, then first install the Postgres server on your machine. For windows, this is the currently active link for downloading the installation package: https://www.postgresql.org/download/windows/
As other answers here pointed out, PgAdmin is only a interface for using the Postgres server. Once you install the server locally, you'll be able to see the server on the left hand side pane after restarting PgAdmin.

pgAdmin4: Unable to connect to Amazon EC2 via SSH Tunnel

I have Amazon EC2 instance running Ubuntu. I have installed and configured PostgreSQL.
Contents of the file /etc/postgresql/9.3/main/pg_hba.conf:
local all all md5
host all all 0.0.0.0/0 md5
Also in postgresql.conf
I have set listen_addresses='*'.
The test command below is successfully starting psql console.
psql -U postgres testdb
Now I am trying to connect pgAdmin4 from MacOS.
I have created a SSH tunnel with following command:
ssh -i ~/.ssh/test.pem -fN -L 5433:localhost:5432 ubuntu#mytestdomain.com
Now I have following details in pgAdmin:
When I save, I get this output:
Unable to connect to server: server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request.
What am I doing wrong ?
Here is the solution,
install pgadmin 4 into your system. and configure the below-added configurations. if the below configurations do not work then please check that DB user permissions on AWS. because of the restriction on IP level so it may not be able to access.