Error connecting to posgresql database using pg on windows - javascript

I've got a PostgreSQL database, definitely existing because I can connect to it and make SQL query using psql.
However, when I try to connect with node.js, using the pg client, I've got an error saying that the database does not exist.
Here is how the connection work:
var pg = require('pg');
const connection = new pg.Client({
host: 'localhost',
port: 5432,
database: 'camino',
user: 'postgres',
password: 'postgres'
})
connection.connect();
And here is the error I obtained:
In english: The database **camino** does not exist.
Can you enligthen me on this error?

Are you sure you have created the database camino by using the create table syntax found here: https://www.postgresql.org/docs/9.1/sql-createdatabase.html
If you have already done that, Postgresql starts with 3 default databases called: postgres, template0 and template1. Have you tried connecting to the default postgres database and confirmed that it works?

Related

How to render rows of data in React.js from mysql database using Express.js?

Can someone explain or point to some tutorial where is explained how to render rows of data from mySql database into react.js component?
I've made small database using mysql workbench and this baza.js file inside my project folder:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'db'
});
connection.connect(function(err) {
if (err) throw err;
connection.query("SELECT * FROM promjer", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
When I run node baza.js in my CMD, everything seems fine, I get everything from that specific table inside CMD terminal so I guess my database is ok and it's connected with app.
What troubles me is how to render that data inside my app?
I know that React by itself can't handle data so i should use Express.js. But I don't get what to do with it. Express should be running on other port so how should I even get data to component in app which is running on port 3000 if express is running on port 9000?
Thanks in advance!

How to authentic connection using Mongoose to MongoDB [duplicate]

I can connect to the DB through terminal, but getting this error using mongoose and gulp.
mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246
MongoError: auth failed
My connection string is:
mongodb://usr:psw#localhost:27017/dbname
Any idea what it can be?
I installed MEAN from MEAN packaged by Bitnami for windows 7 using the following password: 123456
Syntax for connection string to connect to mongodb with mongoose module
mongoose.connect("mongodb://[usr]:[pwd]#localhost:[port]/[db]",{auth:{authdb:"admin"}});
If you don't have {auth:{authdb:"admin"}} in the connection string, you will get the following error: MongoError: Authentication failed.
JS Example: mongo-test/app.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://root:123456#localhost/test',{auth:{authdb:"admin"}});
mongoose.set('debug', true); // turn on debug
just add ?authSource=yourDB&w=1 to end of db url
mongoose.connect('mongodb://user:password#host/yourDB?authSource=yourDB&w=1')
this work for me . &w=1 is important
There is many ways to make it work. This is what worked for me [mongoose v5.9.15] :
mongoose.connect('mongodb://localhost:27017/', {
auth: {
user:'root',
password:'example'
},
authSource:"admin",
useUnifiedTopology: true,
useNewUrlParser: true
})
You might want to do something like this...
var opt = {
user: config.username,
pass: config.password,
auth: {
authdb: 'admin'
}
};
var connection = mongoose.createConnection(config.database.host, 'mydatabase', config.database.port, opt);
'authdb' option is the database you created the user under.
mongoose.connect("mongodb://[host]/[db]", { auth:{
authdb: "admin",
user: [username],
password: [pw]
}}).then(function(db){
// do whatever you want
mongoose.connection.close() // close db
})
Do you have a user set up for dbname? By default, no user is required to connect to the database unless you explicitly set one. If you haven't, you should just try to connect to mongodb://localhost:27017/dbname and see if you still get an error.
I have found the solution hier, looks like when you create an user from the mongo shell, it makes SCRAM-SHA-1 instead of MongoDB-CR. So the solution to create a new user with MongoDB-CR authentication.
MongoDB-CR Authentication failed
just make sure that your database is created.
and also if your user is not added in the admin database, then make sure to add it by putting
db.createUser(
... {user:'admin',pwd:'admin',roles:['root']}
... )
This worked for me for mongod --version = db version v3.6.13
mongoose.connect('mongodb://localhost/expressapi', {
auth: {
authdb: "admin",
user: "root",
password: "root",
}
});
mongo mongodb://usr:psw#localhost:27017/dbname
Password should be alphanumeric only
User should be also available in db 'dbname' (Note : Even if user is super admin)
With above changes it connected successfully.
mongoose.connect("mongodb://[usr]:[pwd]#localhost:[port]/[db]",{ authSource: 'admin', useNewUrlParser: true, useUnifiedTopology: true });
I was getting same error. Resolved by adding authSource option to connect function solved the issue. see above code.
The connection string will be like
mongodb://username:password#localhost:27017/yourdbname?authSource=admin

Cannot Connect to SQL Server after hosting the app on heroku

I'm using mssql along with node.
I call a function to connect to the database:
const sql = require('mssql')
async () => {
try {
const pool = await sql.connect(`mssql://${process.env.DATAUSER}:${process.env.DATAPASS}#${process.env.SERVER}`)
} catch(err) {
console.log(err)
}
}
This works perfectly fine when i'm on a localhost but after i've hosted it to heroku and add the env variables. The error I get is (I changed the ip address to 5's):
ConnectionError: Failed to connect to 55.5.5.555:1433 in 15000ms
My process.env.SERVER = 55.5.5.555/SpecificDatabase and If that means anything
I don't see any reference to your specific database or schema in this code. It may be possible that your local implementation of the data server differs from the heroku implementation in respect to the default schema or database to which a user is directed. Therefore, you may need to add a database or schema field to your connection call.
For example, my pool code to connect to the OSU server is:
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'classmysql.engr.oregonstate.edu',
user : 'username',
password : 'password',
database : 'database_name',
multipleStatements: true
});
module.exports.pool = pool;
Though my implementation here was with mysql and not mssql, it may hold true that you need to explicitly name a specific database or schema.

Connect to MySQL database with Node.js

I'm experiencing an issue when trying to connect to a MySQL database with Node.JS in my React Native application.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
port: 8889,
database: "blabla"
});
con.connect(function(err) {
if (err) console.log(err);
});
I can access to phpmyadmin without any problem (same user and password)
but when I execute the JS file the terminal seems to load but stay stuck like this:
I don't think that there is a link with React but I really have no idea about it.
Thanks in advance for your help
According to the mysql docs: https://www.npmjs.com/package/mysql you need to add con.end(); after you are finished with the connection.
I just understood that I didn't exit the script, but it's working (sorry).
The line to end the script for Node.js beginners like me:
process.exit()

breeze-sequelize with MSSQL possible?

Is it currently possible to connect breeze-sequelize with a MS SQL server?
According to the doc of Sequelize, Sequelize does support MSSQL Server.
Though in the breeze doc there is no MS SQL server listed.
I am a bit confused now. And if it is not possible, is the breeze dev team planning to impl that? Or are there alternatives to use breeze in nodejs with an MSSQL server?
Yes it is actually possible. It took quite some time since the breeze-sequelize documentation is not very "newb friendly".
Here is my configuration for the MS SQL server. The tempHire example from the breeze samples on github was helping out a lot.
var dbConfig = {
user: 'username',
password: 'secret',
dbName: 'myDatabase'
};
var sequelizeOptions = {
host: 'hostname',
dialect: 'mssql',
port: 1433
};
function createSequelizeManager() {
var metadata = readMetadata();
var sm = new SequelizeManager(dbConfig, sequelizeOptions);
sm.importMetadata(metadata);
return sm;
}
The only thing i could not figure out until now is how to communicate with a specific MS SQL instance on a host e.g. localhost\MY_MSSQL_INSTANCE.
UPDATE
I actually did find out how to connect to a specific named instance.
So if you want to connect to an mssql instance like localhost\MY_MSSQL_INSTANCE, the sequelizeOptions should look like the following:
var sequelizeOptions = {
host: 'localhost',
dialect: 'mssql',
dialectOptions: {
instanceName: 'MY_MSSQL_INSTANCE'
}
};

Categories