Connection to remote machine postgresql database - postgresql

I am trying to connect to a VM ubuntu from my local computer. On the VM I have created a postgresql database. Then I followed all steps that I could find on several tutorial when it comes to allow access for remote connexion to the db:
add host all all 0.0.0.0/0 trust to the pg_hba.conf file
add listen_addresses = '*' to the postgresql.conf file
remove the firewall by executing sudo ufw allow 5432/tcp
restart postresql by executing sudo systemctl restart postgresql
By using the command psql and then \c gives You are now connected to database "postgres" as user "postgres".
I then create a password by executing ALTER USER postgres WITH PASSWORD 'password';
Then I use postico to connect locally. I want to connect with this same default superuser postgres for testing. I use the VM ip address as host, db name is postgres user postgres, password is password and port 5432 as mentioned above.
After trying to connect for a while with the message opening connection to server, the result is:
could not connect to server: Operation timed out
Is the server running on host "***.**.**.***" and accepting
TCP/IP connections on port 5432?
Any help please? I did all steps I could find on many tutorials but still failing to connect.

Related

Apache Airflow install Postgress as Backend DB - ERROR

I am trying to install Postgres as a backend DB instead of Sqllite. I followed these steps but ran into an error. can someone help, please?
Steps followed:
activated virtual env.
installed postgreSQL
sudo apt-get install postgresql postgresql-contrib
created psql object with:
sudo -u postgres psql
4.create the user and database for the airflow
CREATE USER **** PASSWORD '****';
postgres=# CREATE DATABASE airflow;
postgres=# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO newt;
updated postgresql.conf with listen_addresses= '*'
added pg_hba.conf with
host db-name user-name ip-address trust
updated airflow.cfg file with
sql_alchemy_conn = postgresql+psycopg2://user-name:password#localhost:5432/db-name
restarted airflow db.
airflow resetdb
After the 8th steps I got below error, what am i doing wrong here? any advise
Error message
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
After changing the pg_hba.conf and postgresql.conf you have to tell PostreSQL that a change has been made. You can do it by either restarting the database, e.g. on Ubuntu
$ sudo service postgresql restart
... or by executing the function pg_reload_conf
SELECT pg_reload_conf():
In case client and database are in different machines, make sure that there is no firewall between them messing things up.
Unrelated note: Also keep in mind that setting a connection to trust in the pg_hba.conf means that no password is required. For testing purposes more than legit but quite dangerous if forgotten in production ;)

connect pgadmin3 to postgres runnng on server

** new to pgadmin and postgress **
my organization uses PostgreSQL on its server with ubuntu os, I try to connect it using pgadmin 3,
these are the stapes I have taken
changed pg_hba.conf using 'sudo nano /etc/postgresql/9.3/main/pg_hba.conf'
changed listen_addresses='*' in postgresql.conf file
sudo ufw allow 5432/tcp
but still, I am unable to connect I get this error from pg admin:
Server doesn't listen
The server doesn't accept connections: the connection library reports could not connect to server: Connection timed out Is the server running on host "103.72.131.155" and accepting TCP/IP connections on port 5432?
how can I fix it,
is there any website which can guide me step by step to connect pgadmin to PostgreSQL.

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

Import PostgreSQL with Sqoop in Docker

I have a PostgreSQL DB sitting on my local machine (Windows) and I would like to import it into my Hortonworks Sandbox using Apache Sqoop. While something like this sounds great, the complicating factor is that my Sandbox is sitting in a Docker container, so statements such as sqoop list-tables --connect jdbc:postgresql://127.0.0.1/ambari --username ambari -P seem to run into authentication errors. I believe the issue comes from trying to connect to the local host from inside the docker container.
I looked at this post on connecting to a MySQL DB from within a container and this one to try to use PostgreSQL instead, but have so far been unsuccessful. I have tried connecting to '127.0.0.1' and '172.17.0.1' (the host's IP) in order to connect to my local host from within Docker. I have also adjusted PostgreSQL's configuration file to listen for connections on all IP addresses. However, I still get the following error messages when I run sqoop list-tables --connect jdbc:postgresql://<ip>:5432/<db_name> --username postgres -P (where <ip> is either 127.0.0.1 or 172.17.0.1, and <db_name> is the name of my database)
For connecting with 127.0.0.1:
ERROR sqoop.Sqoop: Got exception running Sqoop: java.lang.RuntimeException: org.postgresql.util.PSQLException: FATAL: Ident authentication failed for user "postgres"
For connecting with 172.17.0.1:
Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Any suggestions would be very helpful!
If this is just for local testing and not for production level coding, you can enable all trusted connections to your database by updating the pg_hba.conf file
Locate your pg_hba.conf file inside your postgres data directory
Vim the file and update it with the following lines:
#TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 0.0.0.0/0 trust
host all all ::1/128 trust
Restart your postgres service
If you do this, your first use case (using 127.0.0.1) should work

Heroku Postgresql pg:psql No Route to host

For now I am just trying to make the heroku pg:psql command work but my final purpose is to copy a database that I have on my computer (localhost) to the heroku postgresql database with the pg:push command.
For now when I simply try to access the database that I created on heroku, the heroku pg:psql command returns:
psql: could not connect to server: No route to host
Is the server running on host "ec*-**-***-***-**.eu-west-1.compute.amazonaws.com" (**.***.***.**) and accepting
TCP/IP connections on port 5432?
postgresql.conf: (the lines are not commented)
listen_address ='*'
port = 5432
ssl = true
and host all all **.***.***.** trust in pg_hba.conf
I also tried to add rules to iptables in order to give access to the database from the host IP address provided by heroku.
I am on a Debian computer, how can I solve this?
psql: could not connect to server: No route to host
It means your PostgreSQL server is not starting up or is starting up on a different port.
Solutions you may try:
Check PostgreSQL service by command ps -ef | grep Postgres.
Check the port which PostgreSQL is listening to by command netstat -tupln | grep Postgres.
Make sure your server enables UDP port because PostgreSQL needs UDP port loopback for stats collector service.
Check the startup logs or database logs at pg_log about the problem.