Django: authentication for migrations across multiple machines? - python

I have been working on a Django app locally and it's now time to deploy it to a staging machine.
My Postgres database on the staging machine has a different username and password to my local machine.
I have got the Django app running okay on the remote machine, except that the database has not been initialised.
I assume that I should do this with migrate, so I try running:
$ python manage.py migrate
But I see the following error:
django.db.utils.OperationalError: FATAL: no pg_hba.conf entry
for host "127.0.0.1", user "mylocalusername", database "mylocaldbname"
It's failing because it doesn't allow me to log in with mylocalusername.
I assume that mylocalusername etc must be coming from the migrations files? Certainly the local username isn't set anywhere else on the staging machine, either in my settings file, or on the actual database itself.
How can I set up this database on the staging server?
I guess one way would be to delete everything in migrations and create a new local migration. Is that what I need to do?
I thought migrations were supposed to checked into source code, though, so I'd rather not delete all of them. Also, I want to carry on working on locally and updating my staging and production machines, so I need to find a sustainable way of doing this.

"mylocalusername" comes from settings.py file.
It should looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mylocaldbname', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'mylocalusername',
'PASSWORD': 'password',
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
You can change it or create a valid user in your postgres database.

Related

Connecting Django to Postgres: django.db.utils.OperationalError: FATAL: database "DATABASENAME" does not exist

I have just started learning Django after I took some tutorials for Python.
I am trying to connect my POSTGRES database to the Django project I have just created.
However, I am experiencing this problem:
django.db.utils.OperationalError: FATAL: database "producthuntdb" does not exist
I followed these steps:
1) Opened postgress by clicking its icon
2) Clicked on the database "postgress". The terminal opened and I wrote: CREATE DATABASE producthuntdb; The database has been created because I see it if I open postgress via its icon.
3) Went to my Django project in "settings" and change the SQLITE database to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'producthuntdb',
'USER': 'mymac2017',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}
4) Run the code python3 manage.py migrate
However, I am getting the error:
django.db.utils.OperationalError: FATAL: database "producthuntdb" does not exist
So I did the following:
Cliking on postgress icon and opening the the database producthuntdb
Once the terminal is open, I wrote: \du
There are two users with the attributes:
1) mymac2017 | Superuser, Create role, Create DB | {}
2) postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
What am I doing wrong?
I tried to look other answers to this problem and most of the issues are from misspelling the database name OR not creating it. However, the name of my database is correct and I can see the database producthuntdb if I open postgres.
Many thanks for your help.
I set the wrong port of the Database in the Django "settings.py".
I was using 'PORT': '5432' instead of 'PORT': '5433'
SOLUTION:
Go to your Postgres app/icon/program
Click on "Server setting" and see which PORT the database POSTGRES is using
Be sure that the PORT the database is using is the same as the one you write in the "settings.py" of your Django project.

How do I connect development-stage Django web app to an existing AWS MySQL database?

Short question - How do I connect to an existing Amazon Web Services MySQL database from my development-stage Django web app?
Context:
I am working on creating a webpage using the Django framework.
I've successfully created an AWS MySQL RDS database and have imported my data and connected to it successfully via MySQL Workbench.
I am now at a point where I am trying to connect to the database from my Django app. I will require read-only functionality from the database from within the app... I will not be writing any new info to the database. Furthermore, I am trying to do this within a development environment, as I am not even close to anything production worthy (ie, I am working locally off of my laptop).
I've added the AWS database information to my Django settings.py file, and based on my research, to generate the requisite models I'll need to utilize Django's 'inspectdb' utility. I've tried that, and the resulting error received was:
django.db.utils.OperationalError: (1045, u"Access denied for user '[my username was here, which actually showed my literal first name rather than any type of username that was established by AWS]'#'pool-[my IP address was here].nwrknj.fios.verizon.net'
Has anyone else experienced this issue and identified a solution?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'[removed]': {
'NAME': '[removed]',
'ENGINE': 'django.db.backends.mysql',
'USERNAME': '[removed]',
'PASSWORD': '[removed]',
'HOST': '[removed].us-west-2.rds.amazonaws.com',
'PORT': '3306'
Unless you have DATABASE_ROUTERS set to use an alternate database, everything in Django will use the default database. Get rid of the current ENGINE and NAME in default and move all the [removed] db info into default.
Also make sure that the AWS RDS port 3306 is open to your FIOS IP address. Ideally you would restrict it in AWS to only the actual servers (whether on AWS or elsewhere) but for now you may have to set a broad IP range to allow for your FIOS IP changing periodically during development.

Difficulty opening port 5432 for PostgreSQL on Ubuntu 12.04

I'm trying to get a postgres database talking to a django installation.
I've followed the steps details here: http://blog.iiilx.com/programming/how-to-install-postgres-on-ubuntu-for-django/
However, when I use syncdb to have django update my postgres database, I receive the following error:
connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL: database "/home/flowcore/django_db"
does not exist
django_db is the name of the database and it DOES exist but of course it doesn't exist at /home/flowcore/django_db (that is not where postgres stores data!)
My databases dict in settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.path.join(BASE_DIR, 'django_db'),
'USER': 'django_login',
'PASSWORD': 'mypasswordhere', #obviously i've replaced this with my actual password
'HOST': 'localhost',
}
}
Do I have to specific an absolute path to my postgres database file and, if so, where are these files stored?
Well, for some reason you have put the full path as the NAME parameter, so it's not surprising that Django is using that. There's no reason to do that, and that tutorial you link to doesn't suggest it. NAME is the database name itself, which as you say is just "django_db".

how do I confirm django is working with xampp's mysql

I am starting to learn django , and I'm trying to set up a development environment as in http://www.venkysblog.com/setup-django-on-windows-with-apache-and-mysql. I'm having trouble getting it working and so I'm working backwards to make sure I have it all correct.
I'm ok up to step 4. I have confirmed python , xampp and django working . I have created a DB called django using phpmyadmin
I have a project called testproject, with the settings.py file including:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'post
'NAME': 'django', # Or path to database file if usi
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. No
'PORT': '', # Set to empty string for default. Not
}
}
when I run $ python manage.py runserver I get:
Validating models...
0 errors found
Django version 1.4.3, using settings 'testproject.settings'
Development server is running at htp://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Is there an explicit way to test the connection to 'django' db at this point?
Thanks in advance,
Bill
If you do manage.py syncdb it'll try to create your tables, and you'll know if it succeeded or failed.

How to fix "no fixtures found" when installing user management system in Django?

I'm trying to create a database in my first Django project (called "meu_blog"). I've create a file called gerar_banco_de_dados.bat and type the following code inside:
python manage.py syncdb
pause
The code in the settings file of project "meu_blog" is:
DATABASES = {
'default': {
'ENGINE': 'sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'meu_blog.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
But when I tryed to create a user management system with the following information:
•Username: admin
•E-mail: admin#gmail.com
•Password: 1
•Password (again): 1
I get a message saying: “no fixtures found”
When, instead, it should appear this message (according to the tutorial book I'm following):
So, I'd like to know how to fix this "no fixtures found" and get the user management system installed.
I'm using Python 2.6 and Django 1.3.
Thanks in advance for any help.
"No fixtures found" is not an error, per se. That will often show up during syncdb if you're not using initial_data.json fixtures for your apps (which is not required). There is no problem.
The user system is already installed, according to your screenshots.
The fixtures arent installed, mostly because you did not provide any.
See here https://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-data-with-fixtures

Categories