psycopg2.OperationalError connection to server at port 5454 failed Connection refused Is the server running on host and accepting TCP/IP connections - postgresql

Created an app with flask and connected with postgresql via psycopg2 as connector. Created Dockerfile and while building it shows error of
'''Traceback (most recent call last):
File "init_db.py", line 5, in <module>
conn = psycopg2.connect(
File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "192.168.0.167", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?'''
have mentioned host, port, database, username and password in it. Still there an issue. Have also tried changing ports but still the error persists.

Related

Task gets stuck on Airflow when the DB is unreachable

Too many tasks running at the same time on Airflow, some tasks get heartbeat exception and they get stuck. Sometimes even if the tasks are completed, the airflow can't get the completed log.
our setup:
Airflow v2.3.4 in Docker
PostgreSQL 11.6 on x86_64-pc-linux-gnu
example log:
[2022-10-19, 09:01:15 +03] {base_job.py:229} ERROR - LocalTaskJob heartbeat got an exception
....
File "/home/airflow/.local/lib/python3.7/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "xxx" (xxx), port xxx failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
....
return self.dbapi.connect(*cargs, **cparams)
File "/home/airflow/.local/lib/python3.7/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "xxx" (xxx), port xxx failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
(Background on this error at: https://sqlalche.me/e/14/e3q8)

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused

After starting up postgres on docker any attempt to connect to the database results in the following;
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) 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?
Background on this error at: http://sqlalche.me/e/e3q8
I have looked at similar issues and my ports are assigned as such;
ports:
- 5432:5432
Details
Flask server using venv
Windows 10
Additional Details
Dockerfile (for postgres)
FROM postgres:9.6-alpine
COPY init-db /docker-entrypoint-initdb.d/
application file (FLASK_APP) THIS IS THE COMMAND THAT BREAKS IT
#application.cli.command(with_appcontext=True)
def create_db():
db.engine.execute("CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;")
db.drop_all()
db.create_all()
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f04ca2725c96 taxai_ml "gunicorn --worker-c…" 3 hours ago Up 3 hours 0.0.0.0:8900->5000/tcp taxai_ml_1
032fd31432af stripemock/stripe-mock "/stripe-mock -http-…" 3 hours ago Up 3 hours 0.0.0.0:12111-12112->12111-12112/tcp taxai_stripe_1
795b2ff4d7d5 redis:3.2.11-alpine "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:6379->6379/tcp taxai_redis_1

Connection refused to PostgreSQL server (I have two port but it always goes to default)

I am currently setting up a postreSQL server on my Windows computer.(i have 32 bit and 64 bit postgresql)
when I use the (32bits,port is 5433) python to add some data to the database, it gets the following error:
File "C:\Python27\lib\site-packages\psycopg2\__init__.py", line 164, in connection
conn = _connect(dsn, connection_factory=connection_factory, async=async)
perationalError: could not connect to server: Connection refused (0x0000274D/10
61)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
ould 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?
I do write this before adding data to the database
conn=psycopg2.connect("dbname='your_database',user='user', password='password','host'='127.0.0.1','port'='5433')
but it doesn't work~~
thanks!!
You are mixing up your connect() arguments. The "dbname" signals you are using a libpq url, so a single string argument.
http://initd.org/psycopg/docs/module.html
Try fixing that and see if it does the job.

The socket connection to localhost failed..ErrorCode: 10061

When I type localhost on my browser , I am unable to successfully connect..I am getting
The socket connection to localhost failed.
ErrorCode: 10061.
No connection could be made because the target machine actively refused it 127.0.0.1:80
How can I fix it?
You start a web server on your localhost listening on port 80. At present there isn't one.

Zend framework - connection to postgresql

I encountered troubles when trying to connect to postgresql database within Zend framework application. In my application.ini I use
resources.db.adapter = PDO_PGSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = testtest
resources.db.params.dbname = testdb
But when I run the application, I got an error message
Message: SQLSTATE[08006] [7] could not create socket: Address family not supported by protocol.
Could you please tell me what can be wrong?
Zend version 1.12.3., PostgreSQL version 9.0.7, PHP version 5.3.5.
Thanks, Marek .
localhost on your machine is (most likely) resolving to the IPv6 address of localhost (::1), but since the protocol/module used does not support IPv6 connections, your connection fails.
You could either;
Use 127.0.0.1 as a host instead, that will force a connection to the IPv4 localhost.
Remove the host line completely, that will force a connection via UNIX sockets.
I had similar problem:
PDOException: SQLSTATE[08006] [7] 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? in
lock_may_be_available() (line of /app/ctr/ctr
web/drupal/includes/lock.inc).
The problem was caused because of a full disk. and PostgreSQL DB blocked/froze.
Making some free space and restarting of PostgreSQL DB resolved my problem.