I am trying to build Rest API using MS SQL db as backend db.
I installed install django-mssql-backend and defined below config on settings.py
It is a simple Select stmt running on ms sql db and not using any update or insert.
When I try to migrate, it throws below exception that userid should create table permission. Why it is expect full access when i dont have plan to do any write or update operation? How to fix this issue?
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]CREATE TABLE permission denied
in database 'db-name'. (262) (SQLExecDirectW)"))
DATABASES = {
'default': {
'NAME': 'db-name',
'ENGINE': 'sql_server.pyodbc',
'HOST': 'hostname',
'USER': 'username',
'PASSWORD': 'pwd',
"OPTIONS": {"driver": "ODBC Driver 17 for SQL Server", 'unicode_results': True,
},
}
}
Related
I work at the moment on a personal project. I'm using React.js for the front-end and Express.js and PostgreSQL for the back-end.
When I'm trying to register an user in the database, I want the details to be written in 2 tables: 'users'(for generic user details) and 'login'(for keeping user's login details). On the server side I'm trying to achieve this with EXPRESS.js and KNEX query builder npm package to connect to PostgreSQL database. The following code should give a better picture of things:
db.transaction(trx => {
trx.insert({
email: email,
hash: hash
})
.into('login')
.returning('email')
.then(loginEmail => {
return trx('users')
.returning('*')
.insert({
email: loginEmail[0].email,
name: name,
joined: new Date()
})
.then(user => {
res.json(user[0]);
})
})
.then(trx.commit)
.catch(trx.rollback)
})
.catch(err => res.status(400).json(err));
});
After running the app and trying to register an user I recieve the following error back:
{"length":104,"name":"error","severity":"ERROR","code":"42P01","position":"13","file":"parse_relation.c","line":"1384","routine":"parserOpenTable"}
I've checked this error online and got that the following issue is coming up only when a relation doesn't exist in the database at all it could be a mixed-case spelling issue when I created the 'login' table. I dropped the database and created a new one considering the solutions found on the internet and the app is returning the same error.
If anyone encountered with such error or similar case, the help would be very much appreciated!
Loaded proper library at cluster level.
com.microsoft.azure:azure-cosmosdb-spark_2.4.0_2.11:3.7.0
Gave proper connection strings from cosmos table api
cosmosConfig = {
"Endpoint" : "https://cosmos-account-name.table.cosmos.azure.com:443/",
"Masterkey" : "PrimaryKey",
"Database" : "TablesDB",
"Collection" : "Deals_Metadata"
}
Started reading this using spark api.
cosmosdbConnection = spark.read.format("com.microsoft.azure.cosmosdb.spark").options(**cosmosConfig).load()
when i execute this throws below error.
java.lang.NoSuchMethodError: scala.Predef$.refArrayOps([Ljava/lang/Object;)Lscala/collection/mutable/ArrayOps;
I tried to reproduce same in my environment I got same error.
To resolve this error, try to check com.azure.cosmos.spark jar properly installed or not and also follow below code.
Endpoint = "https://xxxx.documents.azure.com:443/"
MasterKey = "cosmos_db_key"
DatabaseName = "<dbname>"
ContainerName = "container"
spark.conf.set("spark.sql.catalog.cosmosCatalog", "com.azure.cosmos.spark.CosmosCatalog")
spark.conf.set("spark.sql.catalog.cosmosCatalog.spark.cosmos.accountEndpoint", Endpoint)
spark.conf.set("spark.sql.catalog.cosmosCatalog.spark.cosmos.accountKey", MasterKey)
spark.sql("CREATE DATABASE IF NOT EXISTS cosmosCatalog.{};".format(DatabaseName))
spark.sql("CREATE TABLE IF NOT EXISTS cosmosCatalog.{}.{} using cosmos.oltp TBLPROPERTIES(partitionKeyPath = '/id', manualThroughput = '1100')".format(DatabaseName, ContainerName))
Reading the data into spark Dataframe
Cfg1 = {
"spark.cosmos.accountEndpoint": Endpoint,
"spark.cosmos.accountKey": MasterKey,
"spark.cosmos.database": DatabaseName,
"spark.cosmos.container": ContainerName,
"spark.cosmos.read.inferSchema.enabled" : "false"
}
df = spark.read.format("cosmos.oltp").options(**Cfg1).load()
print(df.count())
Reference :
Manage data with Azure Cosmos DB Spark 3 OLTP Connector for SQL API | Microsoft
Without changing anything in my settings, I can't connect to my PostgreSQL database hosted on Heroku. I can't access it in my application, and is given error
OperationalError: (psycopg2.OperationalError) FATAL: password authentication failed for user "<heroku user>" FATAL: no pg_hba.conf entry for host "<address>", user "<user>", database "<database>", SSL off
It says SSL off, but this is enabled as I have confirmed in PgAdmin. When attempting to access the database through PgAdmin 4 I get the same problem, saying that there is a fatal password authentication for user '' error.
I have checked the credentials for the database on Heroku, but nothing has changed. Am I doing something wrong? Do I have to change something in pg_hba.conf?
Edit: I can see in the notifications on Heroku that the database was updated right around the time the database stopped working for me. I am not sure if I triggered the update, however.
Here's the notification center:
In general, it isn't a good idea to hard-code credentials when connecting to Heroku Postgres:
Do not copy and paste database credentials to a separate environment or into your application’s code. The database URL is managed by Heroku and will change under some circumstances such as:
User-initiated database credential rotations using heroku pg:credentials:rotate.
Catastrophic hardware failures that require Heroku Postgres staff to recover your database on new hardware.
Security issues or threats that require Heroku Postgres staff to rotate database credentials.
Automated failover events on HA-enabled plans.
It is best practice to always fetch the database URL config var from the corresponding Heroku app when your application starts. For example, you may follow 12Factor application configuration principles by using the Heroku CLI and invoke your process like so:
DATABASE_URL=$(heroku config:get DATABASE_URL -a your-app) your_process
This way, you ensure your process or application always has correct database credentials.
Based on the messages in your screenshot, I suspect you were affected by the second bullet. Whatever the cause, one of those messages explicitly says
Once it has completed, your database URL will have changed
I had the same issue. Thx to #Chris I solved it this way.
This file is in config/database.js (Strapi 3.1.3)
var parseDbUrl = require("parse-database-url");
if (process.env.NODE_ENV === 'production') {
module.exports = ({ env }) => {
var dbConfig = parseDbUrl(env('DATABASE_URL', ''));
return {
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: dbConfig.driver,
host: dbConfig.host,
port: dbConfig.port,
database: dbConfig.database,
username: dbConfig.user,
password: dbConfig.password,
},
options: {
ssl: false,
},
},
},
}
};
} else {
// to use the default local provider you can return an empty configuration
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'sqlite',
filename: env('DATABASE_FILENAME', '.tmp/data.db'),
},
options: {
useNullAsDefault: true,
},
},
},
});
}
I have problems to connect sql server on google cloud to google apps script, have tried many options do url connection like: Jdbc.getCloudSqlConnection("jdbc:google:mysql://apis-para-pap:southamerica-east1:revistamarcasserver","sqlserver", "*****"); but is not connecting, Exception: Failed to establish a database connection. Check connection string.
Do you can help me to solve this problem to connect Sql Server to Google Apps Script?
Information about google cloud Sql Server:
DB Type: SQL Server 2017 Standard
Location: southamerica-east1-b
Instance name: apis-para-pap:southamerica-east1:revistamarcasserver
Public address: 34.95.157.142
White list: (72.14.192.0/18) (64.233.160.0/19) (209.85.128.0/17) (66.102.0.0/20) (74.125.0.0/16) (173.194.0.0/16) (66.249.80.0/20) (64.18.0.0/20) (216.239.32.0/19) (207.126.144.0/20)
(observation: Using sql server management studio, i have tested and connected successfully, with this informations).
Thank you so much
I created a Cloud SQL instance, authorize the following IP ranges and was able to connect to it using the Public Ip Address. I could not connect using Instance connection name.
var db = 'mydatabase';
var instanceUrl = "jdbc:mysql://Public_IP_address_SQL";
var dbUrl = instanceUrl + '/' + db;
/**
* Create a new database within a Cloud SQL instance.
*/
function createDatabase() {
var conn = Jdbc.getConnection(instanceUrl,{user: 'root', password: '****'} );
conn.createStatement().execute('CREATE DATABASE ' + db);
}
/**
* Create a new user for your database with full privileges.
*/
function createUser() {
var conn = Jdbc.getConnection(dbUrl,{user: 'root', password: '****'});
var stmt = conn.prepareStatement('CREATE USER ? IDENTIFIED BY ?');
stmt.setString(1, user);
stmt.setString(2, userPwd);
stmt.execute();
conn.createStatement().execute('GRANT ALL ON `%`.* TO ' + user);
}
/**
* Create a new table in the database.
*/
function createTable() {
var conn = Jdbc.getConnection(dbUrl, {user: 'new_user', password: '****'});
conn.createStatement().execute('CREATE TABLE entries '
+ '(guestName VARCHAR(255), content VARCHAR(255), '
+ 'entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID));');
}
createDatabase()
createUser()
createTable()
}
I have created a simple grails 3 application. I am trying to connect it to an Oracle database in the datasource configuration.
When I run
SELECT * FROM V$VERSION
in sql developer, the following data is returned back about my database.
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
my application.yml file looks like this:
dataSources:
dataSource:
pooled: true
dialect: org.hibernate.dialect.Oracle10gDialect
driverClassName: 'oracle.jdbc.OracleDriver'
username: 'superCool'
password: 'password'
url: 'jdbc:oracle:thin:#127.0.0.1:1521:coolio'
dbCreate: ''
my build.gradle file contains these lines for hibernate and oracle dependencies.
dependencies {
(...)
compile "org.grails.plugins:hibernate:4.3.10.5"
(...)
compile "org.hibernate:hibernate-ehcache"
compile("com.oracle:ojdbc7:12.1.0.2")
}
My service file looks as follows:
class DatabaseService {
DataSource dataSource
public void testMyDb(User user) {
try {
registerUser(new Sql(dataSource), user)
} catch (SQLException e) {
LOGGER.error("unable to register the user", e)
throw e
}
}
public void registerUser(Sql sql, User user) {
sql.call("{call isertUser(?)}", [user.name])
}
If I remove the
compile "org.grails.plugins:hibernate:4.3.10.5"
from the build.gradle, I can run my integration tests and the database is successfully reached. If I keep it there, I get the following error:
ERROR DatabaseService - unable to register the user
java.sql.SQLRecoverableException: Closed Connection
at oracle.jdbc.driver.PhysicalConnection.getAutoCommit(PhysicalConnection.java:2254) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
UPDATE 1:
I updated my build.gradle file to reference
compile("com.oracle:ojdbc6:11.2.0.2")
as opposed to
compile("com.oracle:ojdbc7:12.1.0.2")
and the generated error now refers to the setter:
ERROR DatabaseService - unable to register the user
java.sql.SQLRecoverableException: Closed Connection
at oracle.jdbc.driver.PhysicalConnection.setAutoCommit(PhysicalConnection.java:2254) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
UPDATE 2:
I caught the SQLException and got the sql error code from it. The code returned back: 08003. According to https://docs.oracle.com/cd/E15817_01/appdev.111/b31228/appd.htm ,
08003 - connection does not exist
So at this point, I set the pooled flag to false in the datasource, and everything worked just fine. So the problem here is narrowed down to that. The plugin is not reacting well to the pooled properties.
I have issued the following sql commands to figure out the size of my pool:
SELECT name, value FROM v$parameter WHERE name = 'sessions';
that returns back 1524.
I have also issued the sql command to see the current allocated amount:
SELECT COUNT(*) FROM v$session;
which returns back 58.
I suppose the question now is, what is causing the pooled property to go crazy.
The solution to this was to disable my pooling. I cannot tell if its a bug, r why it fails, but it does. Thankfully for me, I used jndi lookup for my dataSources, so replacing that made the spark.
dataSources:
dataSource:
pooled: false
dialect: org.hibernate.dialect.Oracle10gDialect
driverClassName: 'oracle.jdbc.OracleDriver'
username: 'superCool'
password: 'password'
url: 'jdbc:oracle:thin:#127.0.0.1:1521:coolio'
dbCreate: ''