title: Configure PostgreSQL DB with Vapor 3 on Heroku
body: <p>I've built a simple Vapor 3 API that I'd like to deploy on Heroku. I'd like it to be backed by a PostgreSQL database which is also attached to another Heroku app (I have successfully attached the DB in the Heroku dashboard – and the DB works correctly in the other application). However, my Vapor app never completes starting up, crashing with the following error:</p>
  
  <pre><code>Fatal error: Error raised at top level: ⚠️ PostgreSQL Error: no pg_hba.conf entry for host "[the IP addr]", user "[heroku postgres username here]", database "[heroku psql db here]", SSL off
  - id: PostgreSQLError.server.fatal.ClientAuthentication
  </code></pre>
  
  <p>I used <code>vapor heroku init</code> to set up the Heroku app. I've Googled around, and tried adding a <code>Procfile</code> and messing with <code>configure.swift</code>, but so far no luck. Here are all the relevant files I can think of:</p>
  
  <p>Procfile:</p>
  
  <pre><code>web: Run serve --env production --hostname 0.0.0.0 --port $PORT --config:postgresql.url $DATABASE_URL
  </code></pre>
  
  <p>configure.swift:</p>
  
  <pre><code>import FluentPostgreSQL
  import Vapor
  
  /// Called before your application initializes.
  public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
      /// Register providers first
      try services.register(FluentPostgreSQLProvider())
  
      var contentConfig = ContentConfig.default()
  
      /// Create custom JSON encoder
      let jsonEncoder = JSONEncoder()
      if #available(OSX 10.12, *) {
          jsonEncoder.dateEncodingStrategy = .iso8601
      } else {
          jsonEncoder.dateEncodingStrategy = .millisecondsSince1970
      }
  //    jsonEncoder.keyEncodingStrategy = .convertToSnakeCase
  
      /// Register JSON encoder and content config
      contentConfig.use(encoder: jsonEncoder, for: .json)
      services.register(contentConfig)
  
      /// Register routes to the router
      let router = EngineRouter.default()
      try routes(router)
      services.register(router, as: Router.self)
  
      /// Register middleware
      var middlewares = MiddlewareConfig() // Create _empty_ middleware config
      /// middlewares.use(FileMiddleware.self) // Serves files from `Public/` directory
      middlewares.use(ErrorMiddleware.self) // Catches errors and converts to HTTP response
      services.register(middlewares)
  
      // Configure a database
      let dbConfig: PostgreSQLDatabaseConfig
      if let url = Environment.get("DATABASE_URL"), let psqlConfig = PostgreSQLDatabaseConfig(url: url) {
          dbConfig = psqlConfig
      } else {
          dbConfig = PostgreSQLDatabaseConfig(hostname: "localhost", port: 5432, username: "admin", database: "development", password: nil)
      }
      let postgresql = try PostgreSQLDatabase(config: dbConfig)
  
      /// Register the configured SQLite database to the database config.
      var databases = DatabasesConfig()
      databases.add(database: postgresql, as: .psql)
      services.register(databases)
  
      /// Configure migrations
      var migrations = MigrationConfig()
      migrations.add(model: Visit.self, database: .psql)
      services.register(migrations)
  }
  </code></pre>
  
  <p>Package.swift:</p>
  
  <pre><code>// swift-tools-version:4.0
  import PackageDescription
  
  let package = Package(
      name: "SubwayNyc",
      dependencies: [
          //  A server-side Swift web framework.
          .package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
  
          //  Swift ORM (queries, models, relations, etc) built on PostgreSQL.
          .package(url: "https://github.com/vapor/fluent-postgresql.git", from: "1.0.0"),
  
          .package(url: "https://github.com/vapor/sql.git", from: "2.1.0")
      ],
      targets: [
          .target(name: "App", dependencies: ["FluentPostgreSQL", "Vapor"]),
          .target(name: "Run", dependencies: ["App"]),
          .testTarget(name: "AppTests", dependencies: ["App"])
      ]
  )
  </code></pre>
  
  <p>How can I get PostgreSQL hooked up to my Vapor 3 app on Heroku?</p>