Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Im learning Node and trying create a server using Express and connecting it to a postgres db and keep getting the following when I run node server.js:

events.js:72
        throw er; // Unhandled 'error' event
              ^
error: role "username" does not exist
    at Connection.parseE (/Users/rs/Desktop/Jobletics/node_modules/pg/lib/connection.js:526:11)
    at Connection.parseMessage (/Users/rs/Desktop/Jobletics/node_modules/pg/lib/connection.js:356:17)
    at Socket.<anonymous> (/Users/rs/Desktop/Jobletics/node_modules/pg/lib/connection.js:105:22)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:764:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:426:10)
    at emitReadable (_stream_readable.js:422:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Socket.Readable.push (_stream_readable.js:127:10)

my server.js file looks like this:

// app dependencies
var express = require("express");
var Sequelize = require("sequelize");
var bodyParser = require('body-parser');
var morgan = require('morgan');
var app = express();


//middleware
app.use(bodyParser());
app.use(morgan('dev'));

//sequalize initialization
var sequelize = new Sequelize("postgres://username:password@localhost:5432/jobletics");
var employerRoute = require("./routes/employer")(sequelize);

//sync the model with the database
sequelize.sync().success(function (err) {
    app.get("/employer", employerRoute.get);
    app.post("/employer", employerRoute.create);
    app.listen(5000);
});

I have postgres running. Do I need to create a new db in the command-line then run $psql to create username/password? Shouldn't the db get created automatically?

share|improve this question

You can use

var sequelize = new Sequelize("postgres://username:password@localhost:5432/jobletics");

for connecting to postgresql database, but sequelize does not create nor user nor database for you. You must do it yourself. Use createuser and createdb postgres utilities to create them, or user -c flag of psql command. You also must have privilege to do it, so in the next example commands run using postgres user:

su postgres -c "psql -U postgres -c \"CREATE USER username WITH ENCRYPTED PASSWORD 'password'\""
su postgres -c "psql -U postgres -c \"CREATE DATABASE jobletics WITH OWNER=username ENCODING='UTF8'\""
share|improve this answer
    
Thanks Alex. How do I use createuser/createdb with postgres/sequelize in my code? Don't I need to use sequelize to use the utilities with the 'pg' package? How do you do that? Sorry im new to this stack/postgres. And im assuming once I use the createuser/createdb utility, I can access the DB through psql, correct? Thanks for the help. I really wish there was a tutorial explaining MEAN stack with postgres – rahul2001 Mar 3 '15 at 17:58

I have not seen the database getting created automatically. When I use sequelize.js, i normally run it within a vm which has a puppet manifest to assert that the database already exists.

Also, when it comes to connecting to the db, I would normally do it like so as I think it is easier to read:

var Sequelize = require("sequelize");

var sequelize = new Sequelize(
    "dbName",
    "username",
    "password",
    {
         "dialect": "postgres",
         "port": 5423
    }
);

Finally, make sure that postgres existing in your package.json, if is doesn't you will have to run the following: npm install --save pg pg-hstore

share|improve this answer
    
Can you please explain how you are doing this: "When I use sequelize.js, i normally run it within a vm which has a puppet manifest to assert that the database already exists." – rahul2001 Mar 3 '15 at 18:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.