Django Switching to New Database (locally) - database

Oh my local test for Django I am attempting to switch my default local database from an old SQLLite database to a Postgres database to mimic my live environment hosted through heroku. To do this I have the postgres database locally and I changed all my settings.py as shown below, but Django does not recognize that the DB exists. In addition to this, if I run migrate or syncdb it still tries to recreate the old sqllite3 database. Anybody have any experience with this change from one database to another and see any similar problems?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'postgres',
# Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'myusername',
'PASSWORD': 'mypassword',
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '5432', # Set to empty string for default.
}
}

You are probably using a wrong settings file, or have something like local_settings.py that overrides your settings, or have duplicate DATABASES entries in your settings. Execute:
python manage.py diffsettings
Do you see the correct DATABASES settings there?

Related

ActiveRecord (w/o Rails): How to configure a connection to an azure database

Note - I have never stood up a ruby app like this from the ground up and I am also very unfamiliar with Azure.
I am trying to connect to an existing DB on hosted on Azure via ActiveRecord. I am using ActiveRecord independently of Rails so any convention magic will be unavailable to me. I am currently unable to connect to the database and am unsure if my code configuration is incorrect, if I haven't configured azure correctly, or perhaps my account doesn't have the correct permissions. With my lack of experience I am just unable to determine which is which.
I am currently getting the Login Failed for user '<username>' error from tiny-tds. I've quadruple checked my username and pass and confirmed it is the same credentials I am using to log into the DB from Azure Data Studio. I have also created an app registration from which I got our client id and secret. I'm pretty sure that using the same url for host and resource is not correct but I'm not sure what resource is tbh. With all that said here is my code snippet.
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'sqlserver',
tenant_id: '<tenant_id>',
client_id: '<client_id>',
client_secret: '<client_secret>',
resource: '<servername>.database.windows.net',
active_directory_domain: '<domain> (got this from the primary domain field on the overview screen in azure portal',
host: '<servername>.database.windows.net',
username: '<username>',
password: '<pass>',
database: '<db name>'
)
class IndividualProfiles < ActiveRecord::Base
self.table_name = 'individual_profiles'
end
def run
puts 'Start'
puts IndividualProfiles.first
puts 'End'
end
run
For reference:
activerecord (4.2.11.3)
activerecord-sqlserver-adapter (4.2.18)
tiny_tds (2.1.5)
freetds (v1.3.16)
Not sure exactly what your issue is here (as I do not use all those additional options) however I did notice you are not passing the azure option (which could be the cause of your failure).
There is a configuration in the activerecord-sqlserver-adapter gem specifically for Azure. This is passed through to TinyTDS and without this configuration the connection will not work.
According to the TinyTDS README
TinyTDS is fully tested with the Azure platform. You must set the azure: true connection option when connecting. This is needed to specify the default database name in the login packet since Azure has no notion of USE [database]. FreeTDS must be compiled with OpenSSL too.
My working configurations look akin to:
connection_options = {
adapter: 'sqlserver',
encoding: 'utf8',
pool: 5,
host: '[server].database.windows.net',
database: [DATABASE_NAME],
port: 1433,
azure: true,
timeout: YOUR_TIMEOUT_VALUE,
username: ENV['SQL_USERNAME'],
password: ENV['SQL_PASSWORD']
}
If this does not work please add the following to your post:
Your ActiveRecord version
Your tiny_tds version
Your FreeTDS Version
For Example I have the following working right now: (among other configurations)
ActiveRecord (6.0.4)
tiny_tds (2.1.5)
FreeTDS (v1.00.27 - TDS version 7.3)

Django: "settings.DATABASES is improperly configured" upon migrate, but can access database through ORM

I'm having trouble migrating my database and creating authentication tables to my application's database for superusers, staff, etc. which has to be done by performing python manage.py migrate. I believe my problems are occuring because I'm using legacy databases hosted on a Microsoft SQL Server instance, but I'm not sure and I might just have some things set up wrong.
I expect to connect to the database and add an admin/authentication table to an existing database ('authentication_database'), however I receive the following error message:
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
It should be noted that I can access the client_database's ORM via interactive shell/custom management commands. I simply can not migrate the database.
Here is my DATABASES setting in settings.py:
DATABASES = {
'default': {},
'auth_db': {
'NAME': 'authentication_database',
'HOST': 'host.ip.address.yo',
'PORT': '',
'ENGINE': 'sql_server.pyodbc',
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
},
},
'client_database': {
'NAME': 'client_database',
'HOST': 'host.ip.address.yo',
'PORT': '',
'ENGINE': 'sql_server.pyodbc',
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
},
},
}
# Database routers
DATABASE_ROUTERS = ['app_name.dbrouter.ClientRouter', 'project_name.authrouter.AuthRouter']
DATABASE_CONNECTION_POOLING = False
I can provide my auth routing code if that could help.
I can also provide a full stack trace if that will help. I just don't currently wish to edit out identifying information in the meantime. Thanks!~

MongoDB replication set without restarting database

I have a mongoDB database running in one server. This is its configuration file:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
ssl:
mode: requireSSL
PEMKeyFile: /etc/ssl/mongo.pem
#processManagement:
#security:
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
setParameter:
failIndexKeyTooLong: false
I have created a service to launch the mongoDB each time the server starts or each time the database is down.
This configuration is working so far.
Now I have cloned this server into another one. The configuration is identical except for the server IP and the server domain.
This new server is working too but I would like to connect both databases so the new database is synchronized with the first one as with a master-slave configuration.
I think this is the typical case of a Mongo DB Replication Set with 2 databases. But I’m not very expert with databases and after reading lots of documents I don’t understand very much how to do it.
For example, it seems that all options require to turn off master database before making the synchronization, but in my case the master database is in a production environment so I would like to avoid this. Is there any option to configure the replication set without having to restart the master mongoDB instance?
I’ve checked the reference of the replication options in the connfiguration file too but I don’t know how to use them.
In conclusion, is there any tutorial about how to create a replication set with 2 mongodb databases and if it’s possible without having to restart the master (in production environment) database?

How to connect to MS SQL in Rails

I am attempting to migrate legacy data from a MS SQL database into my Rails Application. I have added configuration in freetds which is connecting properly. In my Gemfile, I have added the tiny_tds and activerecord-sqlserver-adapter, respectively.
I have created a file to house the classes from the legacy database to translate to ActiveRecord:
class LegacyUser < ActiveRecord::Base
establish_connection :legacy
set_table_name 'users'
end
.
.
.
database.yml
legacy:
adapter: sqlserver
mode: odbc
dsn: legacy_db_name
host: db_host_name
database: legacy_db_name
port: 1433
username: username
password: password
Then I have rake tasks to convert the data:
legacy.rake
desc 'migrate users'
task :users => :environment do
require 'lib/tasks/legacy_classes'
LegacyUser.each do |user|
begin
new_user = User.new
new_user.attributes = {
:firstname => user.firstname,
:lastname => user.lastname,
:email => user.email,
:created_at => Time.now,
:updated_at => Time.now
}
new_user.save!
puts "User #{user.id} successfully migrated"
rescue
puts "Error migrating #{user.id}"
end
end
At this point I am just trying to get the rake task to 'connect' to the legacy database.
When I try 'rake users', I get:
rake aborted!
database configuration does not specify adapter
It looks to me like I have clearly specified the adapter. What is the proper way to configure this?
Also as a side question, in my 'classes' file for legacy database tables, should all of those tables mirror the 'new' Rails database schema? Ideally I want to be able to simply connect to various tables in the legacy database and fit them into the new database schema where needed. The associations in the old do not match the new, nor do the naming conventions.
Any help is appreciated. Thanks.
Update
Still experiencing this error. Sadly, the only threads I could find that have had the same error were odd spacing issues in the database.yml file. So I actually took the time to go through and make sure all of the spacing matched my other configurations. Given that it is a vague error I am not really even sure what to check past the instructions for setting up activerecord-sqlserver-adapter.
Got an answer to this here: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/250
'You can try connecting to your DB like so, this works for us on that branch on Rails 4:'
class LegacyTable < ActiveRecord::Base
establish_connection({
:adapter => "sqlserver",
:host => "host",
:username => "user",
:password => "pass",
:database => "dbname"
})
This solved my issue. Hope it helps someone in the future.

Django cannot connect to Postgresql on Opensuse

I've configured everything as i would in another distro (i'm an arch user, and i installed opensuse on a desktop machine).
As :
Django and all it requirements (psycopg)
Postgresql
Created a User for my django server
Created a Database
Granted all power for the database for my new user
Then, when starting syncdb to start working i get this message (I've changed users etc.. in this thread for user:question, password:password, database:question as it is in reality with other words):
FATAL: Ident authentication failed for user "question"
After lurking i got the idea to change my pg_hba.conf file (located at : /var/lib/pgsql/data/pg_hba.conf)
This is what I have:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
My django conf file contains :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'question',
'USER': 'question',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '',
}
}
I don't understand what i missed here, any ideas ?
Edit the /var/lib/pgsql/data/pg_hba.conf file and change the method from trust to md5. This way Postgres will ask for a password.
Also remember to reload the configuration file.
Have you tried changing:
host all all 127.0.0.1/32 ident
to:
host all all 127.0.0.1/32 md5
It appears that you are connecting via TCP (so the first line in pg_hba.confg with "trust" doesn't apply) and authenticate via ident, which requires entries in pg_ident.conf. You probably want "md5" or one of the other password-based authentication options.

Resources