Adding a Postgres Database to a Vapor App

341 Views

Code

configure.swift

    import FluentPostgreSQL

    try services.register(FluentPostgreSQLProvider())

 // Configure a Postgres database
    var databases = DatabasesConfig()
    let databaseConfig: PostgreSQLDatabaseConfig
    if let url = Environment.get("DATABASE_URL") {
        databaseConfig = try PostgreSQLDatabaseConfig(url: url)
    }
    else {
        databaseConfig = PostgreSQLDatabaseConfig(hostname: "localhost", port: 5432, username: "anapaix", database: "postgrestutorial", password: nil)
    }
    let database = PostgreSQLDatabase(config: databaseConfig)
    databases.add(database: database, as: .psql)
    services.register(databases)

    migrations.add(model: Todo.self, database: .psql)



Enjoy this article? Consider supporting VaporForums by following us on Twitter! Get the latest Vapor articles, tutorials, and news.