Forgive me as I this is my first django project using postgresql (version 11.8). For now I just want to connect to a test database which I have set up locally using pgadmin4. When I create the database I am not given the option to add a password, but when I run python manage.py migrate it is insisting on a password. When I then set the password to "password" on pgadmin, django won't accept it. It's probably something really obvious as I am quite new to Django, but I have tried searching and not found the answer to my problem. In settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test1',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Last line of the error when I run python manage.py migrate:
django.db.utils.OperationalError: fe_sendauth: no password supplied
Any help much appreciated. Craig
When you installed PgAdmin4 it asked you to create a password. This is for the superuser postgres, which is what you are trying to connect as. Use psql to connect to the database using the above settings and supply the password and see if it works.
I resolved it by removing the Postgresql 11 server and reinstalling it.
Related
Hello I'm currently trying to implement Buddy with my little StartUp that is using DJANGO REST FRAMEWORK as a base. I found a very good example on the site. Unfortunately in the exmaple the used a MySql DB and I'm using Postgre as DB. My settings.py is:
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'DBname',
'USER': 'DBuser',
'PASSWORD': 'DBpassword',
'HOST': '',
'PORT': '5432',
}
}
}
My Buddy execute look somethig like this:
pip install -r requirements.txt
cd Project
python manage.py test
I also created postgre service in Buddy
like version: latest, Hostname: postgres, Port:5432, Login:DBuser, Password:DBpassword, Name of a database to be created on container startup: DBname
When I run the build Test of my project an error message apears like this:
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
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.5432"?
Action failed: see logs above for details
I really don't now how to fix this, it appears that Buddy creates the database correctly but for some reason django does not reconise that it exists.
Pd: The proyect works fine on my laptop and my friends. I'ts only when I tried to run it on buddy where it gives me problems
import MySQLDB as Database
File "c:\pythonprojects\env\lib\site-packages\mysql_python-1.2.2-py3.6-win32.egg\mysqldb__init__py", line 22
raise ImportError, "This is MySQL Version %s, But _mysql is version %r"
SyntaxError: invalid syntax
Make sure you have pymysql installed. Then in settings.py do this
try:
import pymysql
pymysql.install_as_MySQLdb()
except:
pass
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db-name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
i) First you have to install the mysql server in your machine, if you already have mysql server up and running then jump to next step.
ii) Create a user in mysql (you can use the root user, but I would suggest create a separate one) you can refer mysql documentation for creating a new user. Let, you have created a user with:- username = testuser , password = testpass#123
iii) Create a database in mysql , let named it testDB
iv) Now you have to install mysqlclient-python, to install it from PyPI you can do so by typing pip install mysqlclient (by default pip the python package manager points python2 series if you are setting up your django project on python3 then you have to use pip3) for more information on how to install mysqlclient-python on your particular OS you can refer this link: https://github.com/PyMySQL/mysqlclient-python
v) Now In settings.py under key DATABASES make these changes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # for mysql engine
'NAME': 'testDB',
'USER': 'testuser',
'PASSWORD': 'testpass#123',
'HOST': 'localhost', # if you want to run your project on your localmachine
}
}
vi) Now change your working directory to your django project directory: " cd yourDjangoProject" and run these django commands
python manage.py makemigrations
python manage.py migrate
Now I hope that above django commands will run successfully and you would be able to use to your mysql database in your django project.
I've currently hosted a Django application on EC2 using Apache.
My database engine
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'subscribe',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
After setting up Apache, i can now access my web application on the public IP but I cannot perform any DB transcations as the tables don't exist. This is the error message:
ProgrammingError at /some-url
(1146, "Table 'subscribe.subscriberapp_subscriber' doesn't exist")
I know for sure this is because no migrations have been made post deploying to AWS. My question is, how do I setup the DB completely?
You need to run the initial migrations to create the tables. From the console verify that you have a connection to your db by running ./manage.py dbshell. If that works, you have the connection.
Then you need to either run the initial ./manage.py syncdb (for django <1.7) or if you're running django 1.7+ you will run ./manage.py migrate
i am very new to python Django. i need to connect the MySQL database with python, i really don't have any idea about that ..
i am using python 1.7.
tell me how to connect MySQL database with python. what settings i need to fill in settings.py.
i have goggled it but i am not getting a proper answer please tell me from the beaning how to achieve it.
i have run this command now what are the next steps ?
sudo apt-get install python-mysqldb
i really don't have any idea about the further steps, please guide me
Well you just installed MySQL Database interface for python
Make sure to run pip freeze and see MySQL-python==1.2.5 is there or not .)
And include below snippet in your settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'yourdatabasename',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '',
}
}
I am running python 2.7.3 and django 1.5.1 with postgresql DB. After setting up a simple django app I am getting a OperationalError at FATAL: password authentication failed for user "postgres". When I checked the local variables I found that psycopg2 connection is taking in the wrong password. I had specified the correct password in the django settings.py file.
Also manage.py syncdb works fine, and this problem shows up 2 out 3 times.
All authentication methods in pg_hba.conf is set to md5 and settings.py has HOST: localhost
Don't use the user postgres to do that.
Create a user and a db for your app, and use that user only for that.
su
su postgres
createuser -P <dbusername>
:type and re-type <dbuserpasswd>
createdb -O <dbusername> <dbname>
Check your pg_hba.conf
local all all md5
Restart your postgresql server.
Check your Django Settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dbname', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'dbusername',
'PASSWORD': 'dbuserpasswd',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
Restart your http server.