Django can not connect to PostgreSQL DB - django

When I`m trying to connect Django Server to PostgreSQL db there is an error:
" port 5433 failed: Connection refused Is the server running on that host and accepting TCP/IP connections? "
I`m using Windows 10, Pycharm, Debian
Settings in Django:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'ps_store_db',
'USER': 'zesshi',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5433',
}
}
Tried to check connection with DBeaver and all`s good there, but still cant connect with Django
My firewall is off, i was trying to change from 5432 to 5433
Dbeaver connection
Dbeaver connection 2

Try restarting/reinstalling postgres. Most likely DBeaver has blocked the port that's why you are not able to connect from django.
(Sorry for posting answer, i am unable to comment yet)

The default port of the PG database is 5432. If you need to change this port, you need to edit the postgresql.conf file and restart the database service before the client can access it.
You also need to check the pg_hba.conf file. The recommended configuration is as follows:
host all all 0.0.0.0/0 md5

Related

What do I do to fix my PostgreSQL database connection problem in Django?

I am trying to connect my Django project to a PostgreSQL database I created on AWS but I keep getting this error message each time I run py manage.py runserver.
Here is the error message:
django.db.utils.OperationalError: connection to server at "database-1.ce7oz69tjzjc.us-east-1.rds.amazonaws.com" (52.3.150.111), port 5432 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
I followed the tutorial on w3school and followed all instructions. I created a PostgreSQL database then I updated my settings with the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'codekeen',
'USER': 'baseuser',
'PASSWORD': 'database',
'HOST': 'database-1.ce7oz69tjzjc.us-east-1.rds.amazonaws.com',
'PORT': '5432'
}
}
as I was told to, but I keep getting the error each time try migrating or running the server.
What do I do to fix this?
First of all, make sure that postgresql is up and running. You can check with systemctl status postgresql-xx where xx is your postgresql version. After that,You need to allow TCP/IP connections to be able to connect postgresql database. Open the postgresql.conf file
vim /etc/postgresql/8.2/main/postgresql.conf
or
vim /var/lib/pgsql/data/postgresql.conf
And set the listen addresses to "*" or IP addresses.
listen_addresses='*'
or
listen_addresses='xxx.xx.x.x'

Unable to connect aws RDS to django local

I am trying to connect my local django application to amazon RDS (tried with both MySQL and PostgreSQL) but I am not able to connect as it shows the following errors, Currently seeking answer for PostgreSQL.
In my Settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'database_name',
'USERNAME': 'my_username',
'PASSWORD': 'my_password',
'HOST': 'database-abc.xxx.us-region-yyy.rds.amazonaws.com',
'PORT': '5432',
}
}
In AWS database configuration:
I get this error:
django.db.utils.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "database-abc.xxx.us-region-yyy.rds.amazonaws.com" (69.420.00.121) and accepting TCP/IP connections on port 5432?
I referred to all the available data but still am unable to resolve this issue!
Thanks in advance!
In the VPC security group go to inbound settings create a new rule with the Postgres connection type, not HTTP and make it available from every.
Note: don't forget to make accessibility to the database to be public.

Docker messed with local django/postgres projects. Can't run project on localhost

I stocked at postgresql/django issue. After attempt of running the localhost server got the following error
django.db.utils.OperationalError: FATAL: no pg_hba.conf entry for host "172.17.0.1", database "closerdb", SSL off
According to many similar issues and answers last lines at pg_hba.conf look this way:
host all all 0.0.0.0/0 md5
local replication postgres trust
host replication postgres 127.0.0.1/32 trust
host replication postgres 172.17.0.1 trust
host replication postgres ::1/128 trust
I have restarted postgres after editing .conf file, but error remains. I supposed that it was cause by docker installation, because normally django project has to run on loclahost(127.0.0.1) and 172.17.0.1 is a default docker ip.
How to fix all that and avoid such problems in future?
EDIT: django database settings. worked fine previously before installing docker:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'closerdb',
# 'HOST': '/tmp/mysql.sock',
# 'PORT': '8000',
# 'HOSTNAME': '',
'USER': 'thekotik',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
}
}
I assume your django just wants to connect to a database called closerdb. In this case the corresponding pg_hba.conf entry should look like that:
host closerdb postgres 172.17.0.1 trust
From the postgresql docs:
The value replication specifies that the record matches if a replication connection is requested (note that replication connections do not specify any particular database). Otherwise, this is the name of a specific PostgreSQL database
I also want to point out that you should use trust carefully depending on your environment, because:
Allow the connection unconditionally. This method allows anyone that can connect to the PostgreSQL database server to login as any PostgreSQL user they wish, without the need for a password or any other authentication.

while deplyoing django app on heroku postgres doesn't work

I am trying to deploy my django app on heroku server,i followed the instructions from this website https://devcenter.heroku.com/articles/getting-started-with-python#introduction .it worked fine till , "heroku open" command.When i came to the part where i need to host my database using " heroku run python manage.py syncdb" command , it failed showing the mesage "OperationalError: 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?". I tried lots of fixes including the one suggested here Deploying Django app's local postgres database to heroku? and http://www.cyberciti.biz/tips/postgres-allow-remote-access-tcp-connection.html .I tried all the solutions including editing the "listen_address" = '*' and tcpip_socket='true' in postgresql.conf and editing the ipv4 and v6 values in pg_hba.conf to
host all all 127.0.0.1 255.255.0.1 trust
host all all 10.0.0.99/32 md5
host all all 0.0.0.0/0 .
But none of them worked .I am guessing the problem arises because heroku can not connect to my local postgres server.This is strange because i'm able to access the postgres server via pgadmin.
And also in the django settings.py looks like this
DATABASES =
{
'default':
{
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_test',
'USER': 'postgres',
'PASSWORD': '******',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '5432',
}
}
Do i need to change this and use heroku's database settings instead??
localhost on the server points to the server not your local machine. The reason why is because the server running your django code will try and resolve the dns name localhost and it has a pointer to 127.0.0.1 which is local to the server resolving that name. That will NOT point to your computer you are working on.
You need to get an instance of postgres on heroku and change HOST: 'xxx.xxx.xxx.xxx to the IP address of your new postgres instance in your django settings.

django & postgresql - could not connect to server: Connection refused

I have an os x server running django 1.6 and postgresql 9.
I have just noticed that if I logout of the machine I can no longer connect to my app from another machine.
The error is:
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?
could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting
TCP/IP connections on port 5432?
I have had a look around for a solution but I can't seem to get it to work.
My django settings are:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'myuser',
'PASSWORD': 'mypw',
'HOST': 'localhost',
'PORT': '5432',
}
}
And as I say if I log in to the server with my server user i can then immediately access my web app from another machine. I hope that makes sense. It's as if postgresql runs when my user logs in.
I am assuming that postgresql runs as root / in the background despite whatever user is logged in.
Any ideas?