Heroku postgres: Connection timed out... TCP/IP connections on port 5432? - python

I'm making a new telegram bot and wanna deploy it to Heroku. I made a postgres database with that (heroku). I used to use NordVPN, now I'm not, and I noticed that I can't connect to my database anymore, while using other VPNs. When I run my bot (with other VPNs), I face this error:
Exception: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "ec2-18-211-41-246.compute-1.amazonaws.com" (18.211.41.246) and accepting
TCP/IP connections on port 5432?
It works fine without VPN though. As I searched, It's because of the firewall. But I made windows firewall off, and also paused my anti-virus, and still got the error. Any suggestions on how to make it work with any VPN?
Some information:
>>> systeminfo
...
OS Name: Microsoft Windows 10 Home
OS Version: 10.0.19042 N/A Build 19042
...
>>> python --version
Python 3.8.0
>>> pip freeze | findstr psycopg2
psycopg2==2.8.6
>>> netstat -a -n | findstr "5432"
TCP 0.0.0.0:5432 0.0.0.0:0 LISTENING
TCP [::]:5432 [::]:0 LISTENING
>>> telnet 0.0.0.0 5432
Connecting To 0.0.0.0...Could not open connection to the host, on port 5432: Connect failed
>>> heroku version
heroku/7.54.1 win32-x64 node-v12.21.0
>>> heroku pg:info -a my-bot
=== DATABASE_URL
Plan: Hobby-dev
Status: Available
Connections: 0/20
PG Version: 13.3
Created: 2021-08-08 07:47 UTC
Data Size: 8.1 MB/1.00 GB (In compliance)
Tables: 1
Rows: 15/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
Continuous Protection: Off
The above commands are done bellow a VPN connection
Note: Telegram is blocked in my country, so in order to be able to code telegram bots, I have to use a VPN

I solved my problem by installing OpenVPN and using freeopenvpn configs. I don't know how did that help, but I'm leaving this here for anyone who found this problem in the future

Related

MAVProxy Connect 0.0.0.0:14550

I'm trying to connect Misson Planner so that I can simulate, but getting this error:
(proje) C:\Users\hasan>mavproxy.py --master tcp:127.0.0.1:5760 --out udp:127.0.0.1:20000 --out udp:127.0.0.1:10000
Auto-detected serial ports are:
Connect 0.0.0.0:14550 source_system=255
Loaded module console
Log Directory:
Telemetry log: mav.tlog
Waiting for heartbeat from 0.0.0.0:14550
MAV>
The IP i want to connect to is 127.0.0.1 but it tries to connect 0.0.0.0:14550. I did some research but none of the solutions i found solved my problem.
MAVProxy version = 1.8.35
pymavlink version = 2.4.8
screen shot

Failure to connect to Docker Postgresql instance from Python

I am using Docker to "containerize" a PostgreSQL deployment. I can spin up the container and connect to PostgreSQL via the command line as shown below:
minime2#CEBERUS:~/Projects/skunkworks$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dc176901052a df:pg "docker-entrypoint..." About an hour ago Up About an hour 5432/tcp vigilant_agnesi
minime2#CEBERUS:~/Projects/skunkworks$ CONTAINER_ID=dc176901052a
minime2#CEBERUS:~/Projects/skunkworks$ IP=$(docker inspect -f '{{.NetworkSettings.Networks.bridge.IPAddress}}' $CONTAINER_ID)
minime2#CEBERUS:~/Projects/skunkworks$ echo $IP
172.17.0.2
minime2#CEBERUS:~/Projects/skunkworks$ docker exec -it vigilant_agnesi psql -U postgres -W cookiebox
Passwod for user postgres:
psql (9.6.5)
Type "help" for help
cookiebox#
Now attempting connection with Python:
Python 3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> conn = psycopg2.connect("dbname='cookiebox' user='postgres' host='172.17.0.2' password='nunyabiznes'") Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/minime2/Projects/skunkworks/archivers/env/lib/python3.5/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "172.17.0.2" and accepting
TCP/IP connections on port 5432?
>>>
Can anyone explain why I can't connect to PostgreSQL using Python - even though I'm using the same arguments/parameters that enable a successful connection at the command line (using docker exec?).
[[Additional Info]]
As suggested by #Itvhillo, I tried to use a desktop application to connect to the PG service. I run the docker service using the following command:
docker run -i -p 5432:5432 --name $CONTAINER_NAME $DOCKER_IMAGE
I am using Db Visualizer to connect to the database, and I have set the hostname to 'localhost'. I can successfully ping the port, but still get an error message when I try to connect to the database (possible permissions related error):
An error occurred while establishing the connection:
Long Message:
The connection attempt failed.
Details:
   Type: org.postgresql.util.PSQLException
   SQL State: 08001
Incidentally, this is the tail end of the output for the PG service instance:
PostgreSQL init process complete; ready for start up.
LOG: could not bind IPv6 socket: Cannot assign requested address
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG: database system was shut down at 2018-01-30 16:21:59 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
[[Additional Info2]]
Here is the tail end of my Dockerfile:
# modified target locations (checked by login onto Docker container)
# show hba_file;
# show config_file;
#################################################################################
# From here: https://docs.docker.com/engine/examples/postgresql_service/
# Adjust PostgreSQL configuration so that remote connections to the
# database are possible.
RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf
# And add ``listen_addresses`` to ``/var/lib/postgresql/data/postgresql.conf``
RUN echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf
#################################################################################
EXPOSE 5432
# Add VOLUMEs to allow backup of config, logs and databases
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql", "/usr/lib/postgresql/"]
If you are are running
$ docker run -i -p 5432:5432 --name $CONTAINER_NAME $DOCKER_IMAGE
Then you should be able to connect to localhost:5432 from the host. The easiest way to check whether something is listening on port 5432 is using netcat. In case of success you should get:
$ nc -zv localhost 5432
Connection to localhost 5432 port [tcp/postgresql] succeeded!
In this case, you should be able to connect using:
>>> psycopg2.connect("dbname='cookiebox' user='postgres' host='localhost' password='nunyabiznes'")
If, on the other hand, you get something like:
$ nc -zv localhost 5432
nc: connect to localhost port 5432 (tcp) failed: Connection refused
Then it means that PostgreSQL is not listening, and hence something is wrong in your Dockerfile, and you'll need to post more details on your Dockerfile to diagnose it.
It seems that PostgreSQL couldn't bind socket to listen for TCP connections for some reason. It still listens the default UNIX socket inside the container, though, so you could connect to it via docker exec -it $CONTAINER_NAME psql.
In the postgresql.conf file change
listen_addresses = 'localhost' to listen_addresses = '*'
Then try to connect via psql
psql -h docker-ip -u username -w
Try to isolate the issue: run clean postgres instance without any extensions on default port:
docker run -d --name tst-postgres-01 -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword postgres:9.6
And try to connect to it. If you can, then you have to review your Dockerfile accordingly: just remove everything from there and then add new things one by one. Otherwise, if it doesn't connect, then try to run it on other port:
docker run -d --name tst-postgres-01 -p 45432:5432 -e POSTGRES_PASSWORD=mysecretpassword postgres:9.6
And try to connect. If it works this time, then the issue your network configuration on host machine, somehow 5432 port is blocked or in use by other app.

Running a Self-Developed Python Server on Docker Container

I have this python server:
import SocketServer
class TCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
print 'client is connected'
data = self.request.recv(1024)
print data
self.request.sendall('Message received!')
HOST, PORT = '0.0.0.0', 5004
server = SocketServer.TCPServer((HOST, PORT), TCPHandler)
print 'Listening on port {}'.format(PORT)
server.serve_forever()
and this client:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.56.101', 5004))
s.sendall('Hello my name is John')
response = s.recv(1024)
print 'Response: {}'.format(response)
My OS is macOS and I have Ubuntu 14.04 installed on a virtual machine using VirtualBox. In VirtualBox, I setup a NAT network and I gave Ubuntu this IP address: 192.168.56.101. I put the server program on Ubuntu and added a rule in IPTables to allow incoming connections from port 5004. I started the server on Ubuntu and I tried to connect to the server using the client above on my macOS. The connection went through and the data exchange finished successfully.
Now to the problem. I installed Docker on my virtualized Ubuntu. Docker itself uses another version of Ubuntu 14.04. What I want is to run the server inside the Dockrized version of Ubuntu, so I wrote this Dockerfile:
FROM bamos/ubuntu-opencv-dlib-torch:ubuntu_14.04-opencv_2.4.11-dlib_19.0-torch_2016.07.12
ADD . /root
EXPOSE 5004
CMD ["python2", "/root/server.py"]
I build it using this command: sudo docker build -t boring91/mock and it was built successfully. I ran the Docker container using this command: sudo docker run -p 5004:5004 -t boring91/mock and it showed that it is started listening on port 5004. When I tried to connect to it using my client on my macOS, the socket connected but no data exchange happened. Same thing happens when I run the client on the virtualized Ubuntu. Can anyonw tell me whats the issue here?

Docker container with Python Web App - Connect to Postgres on Host machine (OSX)

I can connect to my local Postgres DB in my web app, but NOT if I am running the web app inside of a Docker container.
Web app running inside of a Docker container
Postgres running in the Host machine
I am not sure if it is related to the Postgres connection settings or to the Docker network settings.
Follow my settings and commands:
Host:
OSX 10.11.6
PostgreSQL 9.6
Docker container
Docker 1.13.1
Docker-machine 0.9.0
Docker container OS: python:3.6.0-alpine
Python 3.6 + psycopg2==2.7
postgresql.conf:
listen_addresses = '*'
pg_hba.conf
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 trust
host all all 172.17.0.0/24 trust
host all all 192.168.99.0/24 trust
With Docker network in HOST mode
docker run -i --net=host -h 127.0.0.1 -e POSTGRES_URI=postgresql://127.0.0.1:5432/db my/image
Error:
could not connect to server: Connection refused
Is the server running
on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
With Docker network in BRIDGE mode
docker run -i --add-host=dockerhost:`docker-machine ip ${DOCKER_MACHINE}` -e POSTGRES_URI=postgresql://dockerhost:5432/db -p 8000:8000 -p 5432:5432 my/image
Error:
server closed the connection unexpectedly
This probably means the
server terminated abnormally before or while processing the request.
Any ideas?
There is a note about doing this in the docs
I want to connect from a container to a service on the host
The Mac has a changing IP address (or none if you have no network access). Our current recommendation is to attach an unused IP to the lo0 interface on the Mac; for example: sudo ifconfig lo0 alias 10.200.10.1/24, and make sure that your service is listening on this address or 0.0.0.0 (ie not 127.0.0.1). Then containers can connect to this address.

Postgresql socket error when running Django's syncdb (mac OS 10.7.5)

I'm trying to run Django's syncdb operation and am getting the following error:
psycopg2.OperationalError: could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I've read the answers to this question and have tried the following:
changing "listen_address" to "localhost" or "*" in /Library/PostgreSQL/9.1/data/potsgresql.conf
changing "host" to "localhost" in my settings.py file (for Django)
user is set to "postgres" with correct password in settings.py
(I restarted the postgresql server after each step.)
I've also checked my pg_hba.conf file to see if the Unix domain socket was accepting connections from all users (based on this page). Here's what I have there:
# "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
I never had this problem before. I somewhat recently upgraded to Lion and am working on a new virtualenv with the bare minimum of modules installed.
Found the answer courtesy of Bradley Ayers:
set "unix_socket_directory" to "/var/pgsql_socket/" in postgres.conf
executed following commands:
"$ sudo dscl . append /Groups/_postgres GroupMembership postgres"
"$ sudo chmod g+w,o+rx /var/pgsql_socket/"
restarted postgresql
your database is not running try
pg_ctl -D /usr/local/var/postgres start
assuming thats where your postgre data directory is

Categories

Resources