I have a PostgreSQL database with dblink extension.
I can use dblink without issues from pgAdmin but not from my nodeJS code using pg-promise.
I have checked that I am on the correct schema and database.
Running SELECT * FROM pg_extension; from my code does return that dblink is installed. However running a query including dblink results in: error: function dblink(unknown, unknown) does not exist
Is there something I should do to make dblink work in this scenario?
This a basic example of my code:
query1 = 'SELECT * FROM pg_extension;'
query2 = `Select *
FROM dblink('host=XXX user=XXX password=XXXX dbname=XXXX',
'select name from example_table')
AS t(name text);`
db.any(
query1
).then(function(results) {
console.log('Query1 result:', results)
}).catch(function(err) {
console.log(`Error in query1 ${err}`)
})
db.any(
query2
).then(function(results) {
console.log('Query2 result:', results)
}).catch(function(err) {
console.log(`Error in query2 ${err}`)
})
Result:
Query1 result: [
{
extname: 'plpgsql',
extowner: 10,
extnamespace: 11,
extrelocatable: false,
extversion: '1.0',
extconfig: null,
extcondition: null
},
{
extname: 'dblink',
extowner: 10,
extnamespace: 2200,
extrelocatable: true,
extversion: '1.2',
extconfig: null,
extcondition: null
},
{
extname: 'timescaledb',
extowner: 10,
extnamespace: 24523,
extrelocatable: false,
extversion: '1.7.1',
extconfig: [
25044, 25042, 25068, 25083,
25081, 25102, 25100, 25118,
25116, 25139, 25155, 25157,
25173, 25175, 25193, 25210,
25246, 25254, 25283, 25293,
25303, 25307, 25324, 25343,
25358, 25472, 25478, 25475
],
extcondition: [
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'WHERE id >= 1000',
'',
'',
"WHERE key='exported_uuid'",
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
''
]
}
]
Error in query2 error: function dblink(unknown, unknown) does not exist
It looks like exactly what I get if dblink is installed in "public" but "public" is not in my search_path.
Related
I have a document in my mongodb database with 'title' = 'Coral Signup Offer' and 'active' = {true}, but when I make the following query, it returns null.
const offer = await Offer.findOne(
{ active: true, title: 'coral signup offer' },
{
collation: {
locale: 'en',
strength: 2
}
}
)
Does anyone know what I'm doing wrong, I'm assuming it's something to do with how I'm using collation as I've never used it before with an await query.
This is my .env
DB_DATABASE="xyz_datatbase"
DB_USERNAME="xyz_user"
DB_PASSWORD="xyz_password"
In index.js i am using above env const but i am getting error password must be string even though it is string, i checked with typeof process.env.DB_PASSWORD and i get string
// config.db.url = mongodb://localhost/
mongoose.connect(config.db.url, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
dbName: process.env.DB_DATABASE || '',
auth: {
user: process.env.DB_USERNAME || '',
pass: process.env.DB_PASSWORD || '',
},
});
Error in console
(node:28764) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: password must be a string
at NativeConnection.Connection.openUri (D:\test\test-app-node-js\node_modules\mongoose\lib\connection.js:830:32)
at Mongoose.connect (D:\test\test-app-node-js\node_modules\mongoose\lib\index.js:335:15)
at Object.<anonymous> (D:\test\test-app-node-js\server\server.js:10:10)
at Module._compile (internal/modules/cjs/loader.js:778:30)
When you store data or env variables in .env file you should not enclose it in double-quotes, your codes should be like this,
DB_DATABASE=xyz_datatbase
DB_USERNAME=xyz_user
DB_PASSWORD=xyz_password
From the documentation
You can specify user and pass at the top level of options, like this
// config.db.url = mongodb://localhost/
mongoose.connect(config.db.url, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
dbName: process.env.DB_DATABASE || '',
user: process.env.DB_USERNAME || '',
pass: process.env.DB_PASSWORD || '',
});
I'm learning how to upgrade databases in an flutter application, apears to be very simple, but I'm getting this error executing an INSERT:
E/SQLiteLog( 9107): (1) table tempsettings has no column named user
I/flutter ( 9107): DatabaseException(table tempsettings has no column
named user (code 1 SQLITE_ERROR): , while compiling: INSERT INTO
tempsettings('id', 'user', 'company', 'url') SELECT 'id', 'user',
'company', 'url' FROM settings) E/SQLiteLog( 9107): (1) no such
column: user I/flutter ( 9107): DatabaseException(no such column: user
(code 1 SQLITE_ERROR): , while compiling: INSERT INTO settings('id',
'user', 'company', 'url', 'theme') SELECT id, user, company, url, 1
FROM tempsettings)
await db
.execute("CREATE TABLE tempsettings("
"'id' INTEGER "
"'user' TEXT,"
"'company' TEXT,"
"'url' TEXT)")
.catchError((error) => print(error.toString()));
await db.execute(
"INSERT INTO tempsettings('id', 'user', 'company', 'url') SELECT 'id', 'user', 'company', 'url' FROM settings")
.catchError((error) => print(error.toString()));
Does anyone could help me how to do this INSERT?
Thanks.
You're missing a comma after the id column creation
await db.execute("CREATE TABLE tempsettings("
"id INTEGER ", //this comma was missing
"user TEXT,"
"company TEXT,"
"url TEXT)")
.catchError((error) => print(error.toString()));
I am setting up REST-API test within my codecept testing framework which uses integrated Chai.
After checking the very basic documentation on the subject in CodeceptJS documentation I can't seem to get my test to work.
const expect = require('chai').expect;
Feature('Checkout');
Scenario('Create a customer', async I => {
const payload = ({
first_name: 'Dummy title',
last_name: 'Dummy description',
email: 'john#test.com',
locale: 'fr-CA',
billing_address[first_name]: 'John',
billing_address[last_name]: 'Doe',
billing_address[line1]: 'PO Box 9999',
billing_address[city]: 'Walnut',
billing_address[state]: 'California',
billing_address[zip]: '91789',
billing_address[country]: 'US'
})
const header = ({Content-Type: 'application/x-www-form-urlencoded'})
const res = await I.sendPostRequest('https://mytesturl-
api.com/api/',payload,header);
expect(res.code).eql(200);
});
I have put my payload and header in a variable for ease of use and readability.
But it doesn't work and keeps giving me Unexpected token [
I figured it out.
The way to format the payload was as a string (see example below)
const expect = require('chai').expect;
Feature('Checkout');
Scenario('Create a customer', async I => {
const payload = ({
first_name: 'Dummy title',
last_name: 'Dummy description',
email: 'john#test.com',
locale: 'fr-CA',
'billing_address[first_name]': 'John',
'billing_address[last_name]': 'Doe',
'billing_address[line1]': 'PO Box 9999',
'billing_address[city]': 'Walnut',
'billing_address[state]': 'California',
'billing_address[zip]': '91789',
'billing_address[country]': 'US'
})
const header = ({Content-Type: 'application/x-www-form-urlencoded'})
const res = await I.sendPostRequest('https://mytesturl-api.com/api/',payload,header);
expect(res.code).eql(200);
});
I have the following in my Sailsjs config/adapter.js:
module.exports.adapters = {
'default': 'postgres',
postgres : {
module : 'sails-postgresql',
host : 'xxx.compute-1.amazonaws.com',
port : 5432,
user : 'xxx',
password : 'xxx',
database : 'xxx',
ssl : true,
schema : true
}
};
And in models/Movie.js:
Movie = {
attributes: {
tableName: 'movies.movies',
title: 'string',
link: 'string'
}
};
module.exports = Movie;
In my controller:
Movie.query("SELECT * FROM movies.movies", function(err, movies) {
console.log('movies', movies.rows);
});
movies.rows DOES return the correct data
However:
Movie.find({ title: 'Frozen' }, function(err, movies) {
console.log('movies', movies)
});
movies returns an EMPTY ARRAY
So it seems all connections are good because the raw query works perfectly.
Could there be something I am doing wrong with setting up the Movie.find() or with models/Movie.js?
Does the tableName attribute not support postgresql schema_name.table_name?
First off, you need to move tableName out of attributes, since it's a class-level property. Second, sails-postgresql does have some (very undocumented) support for schemas, using the meta.schemaName option:
Movie = {
tableName: 'movies',
meta: {
schemaName: 'movie'
},
attributes: {
title: 'string',
link: 'string'
}
};
module.exports = Movie;
You can give that a try, and if it doesn't work, either move your table into the public schema, or nudge the author of the schemaName support for help.