Configure a Database in Vapor

Database is configured in configure.swift file.

  let psqlConfig = PostgreSQLDatabaseConfig(hostname: "localhost", port: 5432, username: "vapor", password: "password")
  var dbConfig = DatabaseConfig()
  let database = PostgreSQLDatabase(config: psqlConfig)
  dbConfig.add(database: database, as: .psql)

Use the same username and password as in Add PostgreSQL into Vapor App.

Vapor docs state that migrations are a way of making organised, testable, and reliable changes to your database's structure-- even while it's in production!

Migrations are often used for preparing a database schema for your models. However, they can also be used to make normal queries to your database.

Configure migrations (also in configure.swift file) :

  var migrations = MigrationConfig()
  migrations.add(model: Tip.self, database: .psql)

Here we just declared that our prosgreSQL database has just one table Tip for out Tip model.

Prev: Add PostgreSQL into Vapor App

Next: Database Relationships

Vapor 3 Tutorial Start

#pub #postgresql #db #vapor #swift