I'm having trouble getting the pg package working on my local system. I've tried to run the following:
var pg = require('pg');
var con_string = "postgres://user:password#localhost:5432/documentation";
var client = new pg.Client();
client.connect(con_string, function(err, res) {
// stuff here
});
But I keep getting TypeError: callback is not a function.
Is there a setting that I need to change in order to connect to the db via a connection string? I have tried the username and password that I'm using in user:password above on a database on my local machine and I can connect just fine.
I've also tried in the node shell in the directory of the project where I installed pg and haven't had any luck.
Thanks
This the error that I get from running the answer below:
$ node pg_test.js
error fetching client from pool { [error: password authentication failed for user "jake"]
Directly from documentation at https://github.com/brianc/node-postgres:
var pg = require('pg');
var conString = "postgres://user:password#localhost:5432/documentation";
//this initializes a connection pool
//it will keep idle connections open for a (configurable) 30 seconds
//and set a limit of 10 (also configurable)
pg.connect(conString, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
//call `done()` to release the client back to the pool
done();
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
//output: 1
});
});
Related
Goal: Do a simple query to the database.
Expected results: "please print something!" and the results from the query are printed on the terminal.
Actual results: Nothing is printed on the terminal.
Errors: No error message.
Here is the db.js file:
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'todoDB'
});
connection.connect();
connection.query('SELECT * FROM categories', function (err, res, fields) {
console.log("please print something!")
if (err) throw err;
console.log(res);
});
connection.end();
I execute this file using:
node db.js
On the mysql cli, I am able to do this query without any problem with the database name, credentials, and query given above.
I know that connection.connect() works since when I'm inputting the code below, the terminal prints "Database is connected!" I think the problem occurs at connection.query, but I am not sure why.
connection.connect(function(err){
if(!err){
console.log("Database is connected");
} else {
console.log("Error while connecting with database");
console.log(err);
}
});
I've looked through all the related questions on stackoverflow and tried them, but none of the solutions seems to resolve the problem that I have.
So it looks like mySQL V8 uses caching_sha2_password as the new authentication standard, which the nodeJS plugin does not support. Connect to your db and try creating a new user that uses the native password auth type.
CREATE USER 'foo'#'localhost' IDENTIFIED WITH mysql_native_password BY 'bar';
I am currently building an API with Swagger on Heroku and i would like to try if the endpoints are creating the correct entries in the Postgre Database. However, once i try to connect to Heroku's Postgre in my testing envoirment the connection is rejected. I think this is because in the continous integration envoirnment heroku creates a sandbox and does not accept connections to the real dbs.
I tried to create a backup database as suggested here: https://devcenter.heroku.com/articles/heroku-postgres-backups
but i couldn't find the information to access!
Any help is appreciated!
Thank you.
Here is my code test:
'use strict';
var should = require('should');
var request = require('supertest');
var assert = require('assert');
var server = require('../../../app');
var pg = require('pg');
var knex = require('knex')({
client: 'postgresql',
connection: process.env.DATABASE_URL
});
describe('controllers', function () {
describe('testing cenas', function () {
it('test1', function (done) {
request(server)
.get('/hello')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
assert.equal(res.body, 'Hello,Hugo Pereira!');
done();
});
});
});
});
it gives an error:
Unhandled rejection Error: Unable to acquire a connection
at Client_PG.acquireConnection (/app/node_modules/knex/lib/client.js:332:40)
at Runner.ensureConnection (/app/node_modules/knex/lib/runner.js:233:24)
at Runner.run (/app/node_modules/knex/lib/runner.js:47:42)
at Builder.Target.then (/app/node_modules/knex/lib/interface.js:39:43)
at testPg (/app/api/controllers/hello_world.js:27:44)
at swaggerRouter (/app/node_modules/swagger-tools/middleware/swagger-router.js:407:20)
at swagger_router (/app/node_modules/swagger-node-runner/fittings/swagger_router.js:31:5)
at Runner.<anonymous> (/app/node_modules/bagpipes/lib/bagpipes.js:171:7)
at bound (domain.js:301:14)
at Runner.runBound (domain.js:314:12)
at Runner.pipeline (/app/node_modules/pipeworks/pipeworks.js:72:17)
at Runner.flow (/app/node_modules/pipeworks/pipeworks.js:223:19)
at Pipeworks.flow (/app/node_modules/pipeworks/pipeworks.js:135:17)
at Pipeworks.siphon (/app/node_modules/pipeworks/pipeworks.js:186:19)
at Runner.<anonymous> (/app/node_modules/bagpipes/lib/bagpipes.js:98:22)
at bound (domain.js:301:14)
0 passing (2s)
The heroku pg:psql command should connect to your database. You can also pass it parameters to connect to alternative/backup databases.
If you are using a heroku postgres url then one thing you need for external access is the query string ?ssl=true appended to the url
e.g.
"DATABASE_URL" : "postgres://user:pass#ec2-54-235-206-118.compute-1.amazonaws.com:5432/dbid?ssl=true"
I manage to solve this problem using the heroku in-dyno database (https://devcenter.heroku.com/articles/heroku-ci-in-dyno-databases). Once i got the database I go to a endpoint in my dev env that i made myself that gives me a dump file of the schemas of my real db. In the test env i import this file and voila i have all the schemas and tables i need for my test envoirment.
I am using aws-sdk with node.js and I am not able to establish a connection to aws ec2 to obtain instance details using the sdk.
This is the error:
Error Error: connect ENETUNREACH
at Object.exports._errnoException (util.js:1020:11)
at exports._exceptionWithHostPort (util.js:1043:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
And this is the piece of code I have written:
I have removed the access keys for security purpose.
var express = require("express");
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: '',
password: '',
database: ''
});
var app = express();
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({
region: 'us-east-1'
});
var AWS_ACCESS_KEY_ID = '';
var AWS_SECRET_ACCESS_KEY = '';
var params = {
DryRun: false
};
ec2 = new AWS.EC2({
apiVersion: '2016-11-15'
});
ec2.describeInstances(params, function(err, data) {
if (err) {
console.log("Error", err.stack);
} else {
console.log("Success", JSON.stringify(data));
res.send({
message: data
});
}
});
app.listen(443);
console.log("The node server is running at port 443");
Is there a way to fix this? I am using aws-sdk for the first time. Thanks in advance.
Probably you're not passing the API keys.
var AWS_ACCESS_KEY_ID = '';
var AWS_SECRET_ACCESS_KEY = '';
var params = {
DryRun: false
};
ec2 = new AWS.EC2({
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
apiVersion: '2016-11-15'
});
Recommendation:
Put your API keys in separated locations.
If this code will be hosted within an EC2, use Service role permissions for EC2.
Use profiles.
The credentials seems to be the issue here. Please double check the credentials you are providing for connecting to EC2. The best option would be to generate a new key_id and secret_access_key and use that.
I would personally recommend to download the credentials file and pass that in your code rather than putting key_id and secret_access_key here.
Is Your Computer or Node.js connect to internet? You control this.
first open cmd an use ping, if the ping return packet proper. Control internet of node.js. For control use requestify module for this. use this code:
const requi=require('requestify');
requi.get("https://google.com")
.then(function(data){
console.log(data);
})
.catch(function (err) {
console.log(err);
});
The easiest way to ensure you're doing this properly is to follow the suggested credential management described in AWS SDK For NodeJs.
npm install aws-sdk
Create a credentials file at ~/.aws/credentials on Mac/Linux or C:\Users\USERNAME.aws\credentials on Windows
[default]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key
And then have the AWS object manage the credentials for you:
var AWS = require('aws-sdk');
It looks like you might be almost there; instead of storing your keys in your code, store them in the specified credentials file above, and try again.
I have a server on sails nodejs and I am trying to connect my controllers with my MySQL db through a wrapper file that would create the connection pool. My purpose is that I use that pool everytime a function in any controller needs to interact with DB, and in such a way that connection is created at the time interaction starts and connection is closed at the time interaction is over. For this, I have created a wrapper file db.js
db.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host:"localhost",
port: '3306',
user:"ye_old_username",
password:"ye_old_password",
database: "ye_old_schema"
});
module.exports = connection;
Now, I am creating a connection pool called ConnectionPool.js
ConnectionPool.js
var mysql = require('mysql'),
config = require("./db");
/*
* #sqlConnection
* Creates the connection, makes the query and close it to avoid concurrency conflicts.
*/
var sqlConnection = function sqlConnection(sql, values, next) {
// It means that the values hasnt been passed
if (arguments.length === 2) {
next = values;
values = null;
}
var connection = mysql.createConnection(config);
connection.connect(function(err) {
if (err !== null) {
console.log("[MYSQL] Error connecting to mysql:" + err+'\n');
}
});
connection.query(sql, values, function(err) {
connection.end();
if (err) {
throw err;
}
next.apply(this, arguments);
});
}
module.exports = sqlConnection;
I have followed the method answered on this question to create the connection pool: How to provide a mysql database connection in single file in nodejs
And finally, I am trying to run a function from a controller using the wrapper and the connection pool. The code inside the Controller is
var connPool = require('./ConnectionPool');
module.exports = {
testConn: function(req, res){
connPool('SELECT * from user where ?', {id: '1'}, function(err, rows) {
if(err){
sails.log.debug(err);
}else{
console.log(rows);
}
});
}
};
All the three files, the wrapper, the connection pool, and the controller are in the same Controllers folder.
Now, when I send a request to the URL through my client, that would invoke the testConn function inside the controller, I get the following response on server log:
[MYSQL] Error connecting to mysql:Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''#'localhost' (using password: NO)
This error is coming from the line connection.connect(function(err) { in connection pool file.
When I try to log on my MySQL db through the same credentials on command line, I am through it. Therefore I believe that db.js file has some format related issue because of which a proper connection is not getting initiated. There can be other reason as well, but the reason I suspect seems to be very strong.
I need some guidance on solving this issue. Any help will be appreciated.
Background Information
I'm attempting my first node.js API/application. As a learning exercise, I'm trying to create some test cases initially delete all records in a table, insert 3 specific records, and then query for those 3 records.
Code
Here's the code I have cobbled together:
http://pastebin.com/duQQu3fm
Problem
As you can see from the code, I'm trying to put the database connection logic in a dbSession.js file and pass it around.
I am able to start up the http server by doing the following:
dev#devbox:~/nimble_node$ sudo nodejs src/backend/index.js
Server started and listening on port: 8080
Database connection successful
However, when I try to run my jasmine tests, it fails with the following error:
F
Failures:
1) The API should respond to a GET request at /api/widgets/
Message:
TypeError: Object #<MongoClient> has no method 'collection'
Stacktrace:
TypeError: Object #<MongoClient> has no method 'collection'
at resetDatabase (/home/dev/nimble_node/spec/resetDatabase.js:6:29)
at /home/dev/nimble_node/spec/e2e/apiSpec.js:23:25
at /home/dev/nimble_node/node_modules/async/lib/async.js:683:13
at iterate (/home/dev/nimble_node/node_modules/async/lib/async.js:260:13)
at async.forEachOfSeries.async.eachOfSeries (/home/dev/nimble_node/node_modules/async/lib/async.js:279:9)
at _parallel (/home/dev/nimble_node/node_modules/async/lib/async.js:682:9)
at Object.async.series (/home/dev/nimble_node/node_modules/async/lib/async.js:704:9)
at null.<anonymous> (/home/dev/nimble_node/spec/e2e/apiSpec.js:19:9)
at null.<anonymous> (/home/dev/nimble_node/node_modules/jasmine-node/lib/jasmine-node/async-callback.js:45:37)
Finished in 0.01 seconds
1 test, 1 assertion, 1 failure, 0 skipped
Database connection successful
Line 6 of resetDatabase is:
var collection = dbSession.collection('widgets');
Given that after the error appears, I get the "Database connection successful" message, I think what's happening is that when the tests request the dbSession library, the database hasn't finished running the code to connect. And therefore, I can't get the collection object.
I'm currently reading through the mongodb online manual to see if I can find some hints as to how to do something like this.
Any suggestions or pointers would be appreciated.
EDIT 1
To prove that there is a collection method on the MongoClient object, I changed the dbSession.js code to look like this:
'use strict';
var DBWrapper = require('mongodb').MongoClient;
var dbWrapper = new DBWrapper;
dbWrapper.connect("mongodb://localhost:27017/test", function(err, db) {
if (!err) {
console.log("Database connection successful");
dbWrapper = db;
var collection = dbWrapper.collection('widgets');
console.log('just created a collection...');
}
});
module.exports = dbWrapper;
And now, when I start up the http server (index.js), notice the messages:
dev#devbox:~/nimble_node$ sudo nodejs src/backend/index.js
Server started and listening on port: 8080
Database connection successful
just created a collection...
It could be an async issue.
Your code in dbSessionjs
dbWrapper.connect("mongodb://localhost:27017/test", function(err, db) {
if (!err) {
console.log("Database connection successful");
dbWrapper = db;
}
});
module.exports = dbWrapper;
Starts the connection at dbWrapper asynchronously, but exports dbWrapper right away, which is then imported in resetDatabase. Thus yes, the connect function may have not yet returned from the async function when you call it in resetDatabase (and is what the log suggests,as the error appears before the success log).
You could add a callback after dbWrapper.connect() returns, in order to actually only be able to use dbWrapper when the connection finished.
(With sqlite, this may not happen as it accesses the DB faster on the commandline).
This may not be your problem but looks like a candidate.
EDIT: Here's a possible example for a callback, but please take note it depends on what you need to do so there are a lot of different solutions. The key is to call a callback function when you are done initializing.
Another solution could be to simply wait, and/or poll (e.g. chcke a variable 'initialized').
'use strict';
var DBWrapper = require('mongodb').MongoClient;
var dbWrapper = new DBWrapper;
function doConnect(callback) {
console.log("Initializing DB connection...");
dbWrapper.connect("mongodb://localhost:27017/test", function(err, db) {
if (!err) {
console.log("Database connection successful");
dbWrapper = db;
var collection = dbWrapper.collection('widgets');
console.log('just created a collection...');
console.log('calling callback...');
callback(dbWrapper);
} else {
console.log("Error connectingi: " + err);
}
});
};
doConnect(function(correctDbWrapper) {
//Now you can use the wrapper
console.log("Inside callback, now consuming the dbWrapper");
dbWrapper = correctDbWrapper;
var collection = dbWrapper.collection('widgets');
});
It's interesting though I never ran into this issue, although I have generally used similar code like yours. I guess because normally I have this DB initialization right at the top, and then have to do lots of initializations on the node app, which gives the app time enough to return from the connect call....