django.db.utils.OperationalError Could not connect to server
It can be some issues:
- PostgreSQL is not running. Check it with sudo
service postgresql status
Your PostgresSQl is not running on port 5432. You can check it typing
sudo netstat -nl | grep postgres
You have something wrong trying to connect to your db like the username, the password or the databasename. Check that they are what postgres ask for you to connect it and that is the db_name that you want to access to.
Problems with postmaster.pid in postgres. It can happen because of a shutdown unproperly done. It makes to remind a pid alive that doesn't allow your server start. To fix it you have to:
* rm /usr/local/var/postgres/postmaster.pid
* pg_resetxlog -f /usr/local/var/postgresAfter this it should run properly if you make the runserver of postgres
Help in Mac OSX: How to start PostgreSQL server on Mac OS X?
django.db.utils.OperationalError: could not connect to server: No such file or directory
This steps worked for me
Deactivate the virtualenv if there is.
pyenv deactivate
Install the required libraries.
sudo apt-get install libpq-dev python-dev
Install PostgreSQL.
sudo apt-get install postgresql postgresql-contrib
Start the psql shell.
sudo -u postgres psql
Setup the postgres user password by entering the following command and you will be prompted for password in the psql shell. Press Control + D to quit after it is done.
\password postgres
Create a new database user called django_user.
`
sudo -u postgres createuser django_user
Go to the psql shell again.
sudo -u postgres psql
Create the database and name it django_db.
CREATE DATABASE django_db;
Set the password for django_user.
ALTER USER django_user WITH PASSWORD '';
Grant the privilege properly.
GRANT ALL PRIVILEGES ON DATABASE django_db TO django_user;
Exit the psql shell and activate the virtualenv.
pyenv activate
Install the psycopg2 package.
pip install psycopg2
Edit the Django project settings.py as follow.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_db',
'USER': 'django_user',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}Initialize the db.
python manage.py migrate
Start the Django project and see if everything works fine.
python manage.py runserver 0.0.0.0:8000
Done =)
django.db.utils.OperationalError: could not connect to server: Connection refused
Change SQL_HOST
to db
in your .envs/.prod
file. This will let the Web container reach the DB container and perform the migration.
Docker compose containers can be accessed with their service name from other containers.
django.db.utils.OperationalError: could not connect to server: No such file on Unix domain socket /tmp/.s.PGSQL.5432?
Sure, you can go to the code and place the pdb
before the self.connection = self.get_new_connection(conn_params)
line, something like:
import pdb; pdb.set_trace()
self.connection = self.get_new_connection(conn_params)
Run the server and once it reaches that pdb
line you could interact with the code in runtime mode, it will show you a pdb
console and then you can see if that environment variables are setted or not:
(pdb) p os.environ.get('DATABASE_NAME')
(pdb) p os.environ.get('DATABASE_USER')
...
It will allow you to see if the variables are setted and with the correct value.
Related Topics
Python Saving Multiple Figures into One PDF File
Is Everything Greater Than None
Convert Alphabet Letters to Number in Python
Query Mongodb on Month, Day, Year... of a Datetime
Python Selenium: Wait Until Element Is Clickable - Not Working
Python Ungzipping Stream of Bytes
Install MySQL-Python (Windows)
Python Pip Install Throws Typeerror: Unsupported Operand Type(S) for -=: 'Retry' and 'Int'
Python Equivalent to 'Hold On' in Matlab
Why Is 'Self' in Python Objects Immutable
Max and Min Date in Pandas Groupby
Panda's Dataframe - Renaming Multiple Identically Named Columns
Same Output in Different Workers in Multiprocessing
Qwidget Does Not Draw Background Color
How to Remove Leading and Trailing Zeros in a String? Python