sails.jssails-postgresql

PostgreSQL setup in Sails


I am new to sails and its connection to database. I plan to use Heroku to host my app so I would like to use Postgresql.

I have changed config.datastore file with the following code:

default: {

        adapter: 'sails-postgresql',
        url: 'postgresql://postgres:postgres@localhost:5432/postgres',
        max: 1
    }

On Sails documentation it sais that url is url: 'postgresql://user:password@host:port/database', but I dont understand where should I get user and password from. Do I need to initiate the postgresql and set it up and define a new database?

I am just trying to understand the mechanics behind this setup.


Solution

  • What you need to do is create a new user for your local PostgreSQL instead of using the default user and database. What I mean is, create a user-specific to your particular project in SQL following this should help https://www.postgresql.org/docs/8.0/sql-createuser.html.(This should also help you set the password)

    Then create the database you want to connect to your Sails app to be used by Waterline by following this: https://www.postgresql.org/docs/9.0/sql-createdatabase.html (make sure you grant the user you created permissions for this database as well https://www.postgresql.org/docs/9.0/sql-grant.html)

    Then all you need is replace the url property value in config/datastore.js with the value following the format

    postgres://USERNAME:PASSWORD@localhost:5432/DATABASE_NAME

    N.B: Replace USERNAME, PASSWORD, and DATABASE_NAME with the actual username, password, and database name respectively.

    When you are deploying to Heroku, Heroku would provide you with an environment variable called DATABASE_URL if you install the Heroku Postgres add-on. You could just pass that to url like so

    url: process.env.DATABASE_URL