when running tests in django and PostgreSQL DB can't create database - python

I'm having a problem with running unit tests in django while using ElephantSQL,
when running command python manage.py runserver everything works just fine,
I'm able to connect to the server without any problem
but when running the command python manage.py test
I'm getting this error below:
C:\Users\Sman9\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\siteid running initialization queries against the production database when it's not needed (for example, when running tests). Djang
warnings.warn(
Got an error creating the test database: permission denied to create database
Creating test database for alias 'default'...
C:\Users\Sman9\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\backends\postgresql\base.py:323: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running
tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
warnings.warn(
Got an error creating the test database: permission denied to create database```
my databases setting in settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '####',
'USER': '#####',
'PASSWORD': '#############',
'HOST': 'chunee.db.elephantsql.com',
'PORT':'',
}
}
}

Make sure that you have granted all required privileges to a user you have created for managing your database.
Using psql you can do so with the following command:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Additionally, you ought to have psycopg2 installed for the postgres DB to pair with Django.
In a terminal execute the following:
pip install psycopg2

Related

Django mysql connection errors

Try to connect my django project with MySQL using wamp server i am facing error
"Django.db.utils.NotsupportedError"
I am tried older version of django
To solve this error, but it seems also
Same error.
To connect Django to a MySQL database, you will need to install a MySQL database connector. The most common connector is mysql-connector-python, which you can install using pip:
pip install mysql-connector-python
Once the connector is installed, you will need to update your Django settings to use the connector and specify the connection parameters for your MySQL database. The settings will typically look like this:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'mydatabase',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
You will need to replace the NAME, USER, PASSWORD, HOST, and PORT values with the appropriate values for your MySQL database.
Once you have configured your settings, you can run the migrate command to create the necessary database tables:
python manage.py migrate
This will create the tables in your MySQL database that are needed by Django. You should now be able to use your MySQL database with Django.

Following Django tutorial, "python manage.py migrate" doesn't create any django tables (MySQL, Pycharm)

In mysite.settings.py I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join(BASE_DIR, 'test'),
#'NAME': 'test',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
}}
When I run 'python manage.py migrate' in the PyCharm terminal, nothing happens, no error message etc. No new tables are created in the database specified. I tried two things for NAME as you can see.
MySQL Server version 8.0.19
MySQL Connector/Python 8.0.19
Python version 3.8
In PyCharm I have package mysqlclient 1.4.6 installed.
Any advice on how to find the problem? How to get an error message?
Thank you
DATABASES property specifies your DB configurations. Now NAME is the property which specifies your database name.
Currently here 'NAME': os.path.join(BASE_DIR, 'test'), won't work as it will give you something like C:/YourUser/YourPath... so this string is not a valid db name. basically (os.path.join() returns a path to your project directory)
First create a db manually on MySql, note the name and put it in property as 'Name': DBNAME in SETTINGS.py.
(Make sure you have your environment set while running following commands)
Then try python manage.py makemigrations for checking migration changes, if everything goes fine up till here then fire python manage.py migrate command.

How to push local django database into heroku database

I have created django project with postgres database I have deployed my project into heroku server, and also migrate with
heroku run python manage.py migrate
I want to push my stored database to heroku database,
PGUSER=edpuser PASSWORD=1qazxsw2 heroku pg:push applications postgresql-curly-07168 --app application
but getting
> error shiv#shiv:~/Documents/projects/django_related_project/zoedp$
> heroku pg:push applications postgresql-curly-07168 --app application
> heroku-cli: Pushing applications ---> postgresql-curly-07168 ▸
> Remote database is not empty. Please create a new database or use
> heroku pg:reset
I also run command heroku pg:reset and again try again
this time I got error
shiv#shiv:~/Documents/projects/django_related_project/zoedp$ PGUSER=edpuser PASSWORD=1qazxsw2 heroku pg:push applications postgresql-curly-07168 --app application
heroku-cli: Pushing edpapplication ---> postgresql-curly-07168
pg_dump: [archiver (db)] connection to database "application" failed: FATAL: Peer authentication failed for user "edpuser"
pg_restore: [custom archiver] could not read from input file: end of file
▸ pg_dump errored with 1
here is my setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'applications',
'USER': 'edpuser',
'PASSWORD': '1qazxsw2',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
}
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)
and overwriting pg_hba.conf deny me.
what should I do?
I was having this issue, is more related to postgresql auth configuration rather than heroku pg:push command
I would recommend this approach:
Set a password for the postgres user:
Based on this amazing answer: https://stackoverflow.com/a/26735105/3172310
At this moment:
You have set a password for the postgres user and configure postgres to ask for id when using psql, which seems pg command does.
then run command as this:
PGUSER=postgres PASSWORD=pg_password heroku pg:push local_db_name DATABASE_URL --app application_name
Replacing:
pg_password: password for the postgres user
local_db_name: your local database
application_name: heroku app name
and should work fine!

Django - Connecting to existing RDS postgres

I am trying to make a Django app to connect to an existing RDS postgres instance
I have changed my settings.py file to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': <db-name>,
'USER': <username>,
'PASSWORD': <pswd>,
'HOST': 'project-hash.us-east-1.rds.amazonaws.com',
'PORT': '5432',
}
}
After creating Django project, running python manage.py migrate yelds:
Tracking file by folder pattern: migrations
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying auth.0001_initial...Traceback (most recent call last):
File "/home/gustavo.figueiredo/anaconda3/envs/lp_admin/lib/python3.7/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation "auth_permission" already exists
manage.py migrate will always fail if it tries to run a migration and finds that any action it's attempting to perform -- in this case, create a table to store user permission data -- has already been done.
If the database instance already exists, you would be much better served making use of manage.py inspectdb to create models from the database, rather than using manage.py migrate to apply models to the database.
If your database is an existing Django database, and you're looking to just get the migrations up and running, the simplest way is to run python manage.py migrate --fake-initial to mark the migration as having been applied without actually performing any database operations other than bumping the migration version.
psycopg2.errors.DuplicateTable: relation "auth_permission" already exists
You do not have a problem connecting. To get this error, you must already be connected.
The problem is that the migration you are trying to run is incompatible with the database you are connected to.

Django syncdb with local_settings - Graphite

I am trying to install graphite on a linux node in a automated fashion. Script that install the graphite, configure and start the services.
Python 2.7
Django 1.5.12
One of the step to install graphite need **
manage.py syncdb
**
Earlier I was trying to do in interactive way where I was able to create super user .
python /opt/graphite/webapp/graphite/manage.py syncdb
Once installed it has following tables.
account_mygraph auth_user_user_permissions
account_profile dashboard_dashboard
account_variable dashboard_dashboard_owners
account_view django_admin_log
account_window django_content_type
auth_group django_session
auth_group_permissions events_event
auth_permission tagging_tag
auth_user tagging_taggeditem
auth_user_groups url_shortener_link
I would like to make it automated by providing my own settings in local_setting.py file.
I have following in my file.
DATABASES = {
'default': {
'NAME': '/opt/graphite/storage/graphite.db',
'ENGINE': 'django.db.backends.sqlite3',
'USER': 'root',
'PASSWORD': 'Pa55word',
'HOST': 'localhost',
'PORT': ''
}
}
When I run following
python /opt/graphite/webapp/graphite/manage.py syncdb
--settings=local_settings
It execute with following output
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
It do not create tables and auth users etc.
sqlite> .tables list
sqlite>
How do I have all tables by providing my local_settings through local_setting.py file.
Please advise.
First you need to migrate to resemble the new db.
python /opt/graphite/webapp/graphite/manage.py migrate --settings=local_settings
and then syncdb. Also django might ask for makemigrations

Categories

Resources