Difficulty opening port 5432 for PostgreSQL on Ubuntu 12.04 - python

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".

Related

Can not connect to DB, after deleting old DB and all migrations

I replaced all URLFields in the models with Cloudinary fields, and decided to drop the DB and make new, because issues appeared. So every migration file, accept init files was deleted, I dropped the DB, even deleted all containers and volumes(I use docker for PostgreSQL). I have made new images and containers in docker. New database. But still can't connect to database nad migrate.
This is the error:
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more det ails.
This is my DB Settings:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'fishbook_db', 'USER': '*******', 'PASSWORD': '********', 'HOST': 'localhost', 'PORT': '5432', }, }
I have tried to fix the problem, by searching for this error in the web. I see a lot of people have the same problem, but I wasn't able to find solution so far.

`manage.py inspectdb` tells me that I don't have an ENGINE specified in my default database, but the docs say that default can be blank

I'm creating a django web app that is going to be a database management portal for multiple databases. Because the web app will touch multiple databases, it doesn't make sense to have a default. However, when I run manage.py inspectdb I get an error saying that the ENGINE value isn't set on my database. It most definitely is.
Here's my DATABASES setting in settings.py
DATABASES = {
'default': {
},
'my_db': {
'NAME': 'my_db',
'USER': 'user',
'PASSWORD': 'pass',
'HOST': '192.168.0.255',
'PORT': '',
'ENGINE': 'sql_server.pyodbc',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
If I run manage.py inspectdb using this setup I get this error:
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
This doesn't make sense to me, since it says in the documentation that 'default' can be a blank {}.
However, if I supply a dummy NAME and ENGINE variable to the default db, it seems to work fine for the default DB, but it ignores my_db.
If I set default to look at my_db's information I get a login error (so I know at least something is working right there, even if my creds are bad).
So, what am I getting wrong in my database setup here?
You need to specify a database for which you need to inspectdb
python manage.py inspectdb --database your_db_name
For more details see the docs

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.

Django: authentication for migrations across multiple machines?

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.

Django: How to set the correct the location for MySQL database

I am just starting to use MySQL as the database for my project. Previously I had been using SQLite.
I am wondering how to specify the location for the MySQL database the same way I was able to for SQLite. Currently it saves automatically to /usr/local/mysql/data by default. But I believe this will cause issues when I try to upload it to my production envroment.
My old SQLite settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'database/db.sqlite3'),
}
}
This saved the database into a database folder in my project which I was able to upload and chown both to www-data. This seems like a simple solution which I would like to replicate with MySQL
My new MySQL settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
#'NAME': 'django_db',
'NAME': os.path.join(BASE_DIR, 'database/django_db'),
'USER': 'root',
'PASSWORD': 'myPassword',
'HOST': 'localhost',
'PORT': '3306',
}
}
However when I try to syncdb with this I get the below error
django.db.utils.OperationalError: (1059, "Identifier name
'/users/user/workspace/bias_experiment/src/database/django_db'
is too long") (bias_experiment)localhost:src user$
I also tried to create the database within my project with
mysql> CREATE DATABASE /Users/user/workspace/bias_experiment/src/database/django_db;
But this gave me the error
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/Users/brendan/Dropbox/workspace/bias_experiment/src/database/django_db' at line 1
mysql>
So can I create the DB with in my project and link to it in the same way as I did previously with SQLite? And If not what is the correct way/location to upload it to and link to it?
Any help is greatly appreciated
MySQL is not a file-based database: you don't give it a file path. In fact the settings file itself is quite clear about what you need to put in the NAME attribute, ie (not surprisingly) the name of the database itself. This is the same as you need for CREATE DATABASE, but again you don't give that a path: just a name.
You cannot access mysql files directly. You must create a database (give it a name, not a path) and access it.
SQL> CREATE DATABASE django_db

Categories