why can't i connect to postgres on AWS EC2? - postgresql

I'm hoping someone who has experienced this before can help me get this connection to AWS/EC2 -> Postgres working.
All the following is via terminal, on a Mac client.
The database in question - Postgres - is running remotely on this EC2 server, not, on my local Mac (nor is Postgres installed on my local Mac)
My understanding is that I need to be able to actually connect to the EC2 server and make an SSL "tunnel" connection -: from my PC to this same EC2 server -- so I can then, access the Postgres database
If my understanding there is wrong please let me know.
So, I have 2 terminal windows open. Terminal A, and Terminal B.
In Terminal A
(a) connecting to the EC2 server works fine. Here's the CLI command:
ssh -L 3307:127.0.0.1:3306 -i my.pem ubuntu#xx.yy.zzz.99
The CLI in Terminal A then shows me the Ubuntu/server prompt
Last login: Wed Oct 25 12:27:31 2017 from aaa222-444.xxxx.rrr.ff.net
ubuntu#ip-111-222-333-44:~$
then, I go to terminal B...
In Terminal B
(b) i try to make a connection to Postgres from local using the following command:
psql -h localhost -p 3307 -U postgres_uid postgres_databasename
THIS FAILS EVERY TIME.
I get this response in Terminal B:
psql: received invalid response to SSL negotiation: [
I am wondering, is there is a DIFFERENT SSL setup that is needed to make this DATABASE connection ? Or should it be that once I am logged into the AWS EC2 server (as noted above) I should be able to run psql commands (or use GUI clients like Navicat etc) ?
I'm at a loss and no one here knows how to fix this / set this up. it's probably something simple but that's fine, I just need to get connected :-)
Appreciate anyone's help with this thanks.

Use URI syntax in the -d option, and the SSL should take care of itself.
I use the following command to access cloud postgres instances (on bluemix, not ec2), give it a go...:
psql -d postgres://<user>:<password>#<host>:<port>/<dbname>

Related

pgAdmin4: remote access to PostgreSQL server

I can access the remote PostgreSQL server in the lab from both the terminal and using pgAdmin4 when connected to same private network.
From Terminal:
PC:~$ psql -h 193.13x.xx.xx -U myusername -W dbname
#then password after prompt
Using pgAmin4:
Host name/address: 193.13x.xx.xx
Port: 5432
Maintainance database: dbname
Username: myusername
Password: *******
However, when I switched to another network eduroam, I can only connect to the remote server via the terminal, not from pgAdmin4. So it is not very easy to work away from the lab.
Is there a workaround to enable connecting via pgAdmin4?
I can only connect to the remote server via the terminal, not from
pgAdmin4. So it is not very easy to work away from the lab.
Is there a workaround to enable connecting via pgAdmin4?
Yes. The generic answer to that is: use an SSH tunnel. It's very usual when you cannot access directly a database from outside a certain network, which appears to be your case based on the error message in the comments.
If you need specific advice for pgAdmin4, consider searching for "pgadmin4 ssh tunnel", there are many tutorials available online.

Accessing WSL postgresql server from Windows PGadmin

I am running postgresql in WSL Ubuntu on windows. Everything is up to speed, my data is loaded and I wish to access the database through some graphical interface. I was thinking pgadmin4.
Is it possible to accomplish this through a windows install of pgadmin4? I installed pgadmin4 on windows and tried to connect the traditional way in the GUI through localhost but am not getting a connection. I figure there may be a special method here.
For everyone else stumbling across this: The best way to do this (that I know of) is to SSH into your local WSL and then configure the SSH in pgAdmin to that.
I have yet to figure out, how to use this remotely.
As long as postgres is running within your wsl2 instance (check with sudo service postgresql status) then within PgAdmin 4 (running in windows), all you need to do is click to "Register" a new server.
Then, while entering the Connection data, set host to localhost and port to 5432 (unless you specified unique port within your postgres instance when creating in wsl2.
See this answer for more detail
There is no special way needed, you should be able to get a connection. Just like your dev http ports are exposed to your browser on Windows, your db port should too.
I had to manually add localhost to pg admin though which is a bit weird.
Make sure your db service is up and running on Ubuntu, sometimes the db service is killed for no reason.
To see if your PostgresSQL service is up or not:
sudo service postgresql status
If it's not, start the service:
sudo service postgresql start

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.

SSH tunnel to postgres from Tableau desktop - invalid username and password

I am trying to connect Tableau to a postgres DB using a SSH tunnel. The steps I have taken are
Install postgres drivers (32 bit) on my desktop
Create the tunnel in putty (tunnel L5432 127.0.0.1:5432) and open up the terminal
From this session I can run Postgres from the command line psql -d mydb -U myuser
However, when trying to connect using Tableau I get the error message "Invalid username and Password"
If I try to connect to a DB that doesn't exists I get the same error message "Invalid username and Password"
I have also created a psql user with superuser privileges but no success. Note that the username for the ssh tunnel and the psql db are different.
I have seen a number of posts on the forum with no solution.
Can anyone help? Thanks.
Roger
I met with the same question, with slight difference, I didn't use PuTTY but instead using Cygwin Autossh.
The answer is that two tunnels need to be built, first an ssh tunnel from local host to server, and then from the server port to the database port. A possible solution is to set up ssh tunnel like this:
5432:localhost:5432 username#hostserverdomain
The first 5432 is the local port(Tableau or PGAdmin), the second localhost actually refers to the server(cuz once log in localhost means the server itself), the second 5432 is the database on the server. And in order to get access to the database, one has to log in the server.
The above is just the syntax to connect thru two tunnels using ssh.
At first I didn't understand what Roger means by two tunnels, and then I look into the underlying methodology and finally got all the setup work done. Thanks Roger!

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