Django syncdb with local_settings - Graphite - python

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

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.

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

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

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.

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 py manage.py makemigrate

What can I make with it? I'm beginner in python and django. I download it and I i wrote py manage.py makemigrate and I've get error. Can u help me?
Your issue is with your DB configuration in the setting.py. If you are using the default SQLite then copy/paste this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
and your project will work just fine. After this, run
python manage.py makemigrations
python manage.py migrate #copy all migrations to the database
python manage.py createsuperuser #to have a admin user to login to adminpanel
python manage.py runserver #starting the server
Otherwise, take a look at the official documentation how to connect MySQL, PostgreSQL, Oracle databases and required configurations.
Your error is in here:
SQLite is not like MySQL or other databases. Actually, it is not a real database. You are using a port, username, password and etc. These are the cause of the error. SQLite is not running in the server or another place. It is just a single file contains data information. Update yours to mine above and it should start work again or change your database to MySQL or others.
You need to supply all environment variables that are listed in your settings file. Such as DB_NAME that presented in your screenshot. Search for os.environ[<VARIABLE_NAME>], every VARIABLE_NAME should be defined.
If you are a beginner it is better to stay with the documentation and do like https://docs.djangoproject.com/en/2.1/intro/tutorial01/
If you could share the DB part of the settings.py it would help.
Generally python manage.py startapp appname should create the necessary files for you.
After which a python manage.py makemigrations and python manage.py migrate should work properly. And this should not come.

Categories

Resources