Home > Enterprise >  Unable to put Vapor project with Postgres database on a VPS
Unable to put Vapor project with Postgres database on a VPS

Time:08-07

I'm a newbie with database and Vapor. I'm trying to put a vapor project on a Postgres database on a VPS but I had this error:

2022-08-05 11:18:26.611154 0200 Run[16315:261688] Swift/ErrorType.swift:200: Fatal error: Error raised at top level: PostgresNIO.PSQLError(base: PostgresNIO.PSQLError.Base.connectionError(underlying: NIOPosix.NIOConnectionError(host: "82.125.176.210", port: 5432, dnsAError: nil, dnsAAAAError: nil, connectionErrors: [NIOPosix.SingleConnectionFailure(target: [IPv4]82.125.176.210/82.125.176.210:5432, error: connection reset (error set): Operation timed out (errno: 60))])))

Here my code:

import Fluent
import FluentPostgresDriver
import Vapor
import Leaf

public func configure(_ app: Application) throws {
    app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
        app.middleware.use(app.sessions.middleware)
    let databaseName: String
    let databasePort: Int
    
    databaseName = "vapor_database"
    databasePort = 5432

    app.databases.use(.postgres(
    
    hostname: "82.125.176.210",
    port: databasePort,
    username: Environment.get("DATABASE_USERNAME") ?? "456787555",
    password: Environment.get("DATABASE_PASSWORD") ?? "456787555",
    database: Environment.get("DATABASE_NAME") ?? databaseName
  ), as: .psql)
  
    app.migrations.add(CreateUser())
    app.migrations.add(CreateAcronym())
    app.migrations.add(CreateCategory())

    app.migrations.add(CreateAcronymCategoryPivot())
    app.migrations.add(CreateToken())

    switch app.environment {
    case .development, .testing:
    app.migrations.add(CreateAdminUser())
    default:
    break
    }
    
    app.migrations.add(AddTwitterURLToUser())

    
    app.migrations.add(MakeCategoriesUnique())
  
    app.logger.logLevel = .debug
  
    try app.autoMigrate().wait()

    app.views.use(.leaf)
  
    // register routes
    try routes(app)
}

CodePudding user response:

A timeout error usually means that there's no route to the database server because it's being blocked by a firewall. (As opposed to a connection refused error if there's nothing listening on that port). Make sure you set a rule in the firewall to allow the Vapor app to connect

  • Related