How do I connect my Django App to Postgres Databse? - django

How do I connect my Django App to Postgres Databse?
Whenever I run python manage.py makemigrations after the changes, then I get this kind of error. How can I fix it?
Got an error checking a consistent migration history performed for database connection 'default': connection to server
at "localhost" (::1), port 5432 failed: Connection refused (0x0000274D/10061)
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused (0x0000274D/10061)
Is the server running on that host and accepting TCP/IP connections?
warnings.warn(
No changes detected

Seems like your postgres server is not running. I would suggest you to ensure that postgres server is running and you are able to connect to it.
You can use your terminal or any DB tool like PgAdmin, DBeaver, Tableplus etc.
After you are able to connect to your database through any of the above mentioned ways. Django db connection should not be a problem.
You can take help from
https://tableplus.com/blog/2018/10/how-to-start-stop-restart-postgresql-server.html
https://www.enterprisedb.com/postgres-tutorials/connecting-postgresql-using-psql-and-pgadmin

Related

connection to server at "localhost" (127.0.0.1), port 5432 failed

I am facing this error "OperationalError at /results/
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
Is the server running on that host and accepting TCP/IP connections?" when I try to run a django application that I just deployed in railway.app.
What is the problem and how can I fix?
When I run the application locally it works well, it is my first time deploying a django application to production and I have no idea how to solve that issue, your help is greatly appreciated.
I just realized that the error is due to not using railway's database. Apparently it was running localhost and my postgres' port, created a postgres database in railway and connected to the deployed django app and it is now working well. Thank you #Laurenz Albe for reminding me that.

How to Access Remote PostgreSQL Database Server Through VPN

I'm currently working with my office server, when i want to connect to the server, i have to use their VPN. I have installed PostgreSQL database in their server and have succesfully migrate data to database from Django project. But, when i want to remote access from HeidiSQL, it always gives me Connection Timed Out Error eventhough i have already connected to their VPN.
I've tried to this code below
/var/lib/pgsql/14/data/pg_hba.conf
host all all 0.0.0.0/0 md5
host all all ::/0 md5
/var/lib/pgsql/14/data/postgresql.conf
listen_addresses = '*'
i have tried to refresh everytime i change files and see if i'm in the correct port but it still gives me Connection Timed Out Error
this is the full error message
could not connect to server: Connection Timed out (0x0000274C/10060). Is the server running on host "xxx.xxx.xxx.xxx" and accepting TCP/IP connections on port 5432 ?
NOTES
OS : CentOS 8
DB : PostgreSQL 14

Database connection Error while hosting site on heroku

So I was trying to host my first Django app on heroku.
Took help form some Youtube channels. I used postgresql as my database. Now when I tried hosting my app on heroku the following error is showing up:
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?```
**This is the error showing**
run postgres -D /usr/local/var/postgres The result of this should be:
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 379) running in data directory
"/usr/local/var/postgres"?
Then type kill -9 PID in HINT
Let me know if this works for you?

Won't create a PostgreSQL database on Unix server

I am trying to deploy my django project to a unix server. When I run the command to create the PostgreSQL database using SSH, I encounter the following error:
$ createdb elections
createdb: could not connect to database template1: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Does the error mean running 'locally' as in the server or my machine?
What is stopping it from connecting to the server?
How do I fix the error?

django website working, but django manage.py cannot connect to db

I'm trying to run a django migration to update my database, but "manage.py migrate" can't connect to my postgresql db. The odd thing is: my django website (which is exactly the same code) is working including connecting to the db.
Error messages:
psycopg2.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.xxxxxx"?
and
django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL
Workaround: Change HOST variable to either localhost or 127.0.0.1 .
see: https://community.webfaction.com/questions/21072/django-website-working-but-django-managepy-cannot-connect-to-db