Skip to content

Instantly share code, notes, and snippets.

@theptrk
Last active January 21, 2019 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theptrk/935fde22b2d758d98df98c5ba6e1fbbd to your computer and use it in GitHub Desktop.
Save theptrk/935fde22b2d758d98df98c5ba6e1fbbd to your computer and use it in GitHub Desktop.
Migrate a postgres database with sequelize.js

Migrate postgres database with sequelize.js

Part I: Set up postgres and sequelize

  • Start the postgresql server using brew and ensure relaunch on login more
$ brew services start postgresql

If needed, install postgresql with $ brew install postgresql and note the version installed.

  • Create a postgres database to use
$ psql postgres
psql> CREATE DATABASE kale;

Note the ending semi-colon ;

  • Install postgresql sequelize and sequelize cli for your project
$ npm i --save pg sequelize sequelize-cli
  • Run the sequelize init command
$ node_modules/.bin/sequelize init

This will create the folders config, migrations, models and seeders

Part II: Create and run migrations

  • Create the model and migration files
$ node_modules/.bin/sequelize model:generate --name User \
  --attributes firstName:string,lastName:string,email:string

This will:

  • Create a user model file in our models directory

  • Create a timestamped migration file in our migrations directory

  • Run the migration

$ node_modules/.bin/sequelize db:migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment