postgresqldartaqueduct

Dart Aqueduct server database upgrade: error connecting to the database


I'm setting up a PostgreSQL database for my Aqueduct server. I created a user and database with psql:

CREATE DATABASE words;
CREATE USER words_user WITH createdb;
ALTER USER words_user WITH password 'password';
GRANT all ON database words TO words_user;

My model class is

import 'package:demo/demo.dart';

class Word extends ManagedObject<_Word> implements _Word {

}

class _Word {
  @primaryKey
  int id;

  @Column(unique: true)
  String word;

  @Column()
  String info;
}

I generated a migration file with

aqueduct db generate

which is:

import 'dart:async';
import 'package:aqueduct/aqueduct.dart';

class Migration1 extends Migration {
  @override
  Future upgrade() async {
    database.createTable(SchemaTable("_Word", [
      SchemaColumn("id", ManagedPropertyType.bigInteger,
          isPrimaryKey: true,
          autoincrement: true,
          isIndexed: false,
          isNullable: false,
          isUnique: false),
      SchemaColumn("word", ManagedPropertyType.string,
          isPrimaryKey: false,
          autoincrement: false,
          isIndexed: false,
          isNullable: false,
          isUnique: true),
      SchemaColumn("info", ManagedPropertyType.string,
          isPrimaryKey: false,
          autoincrement: false,
          isIndexed: false,
          isNullable: false,
          isUnique: false)
    ]));
  }

  @override
  Future downgrade() async {}

  @override
  Future seed() async {
    final rows = [
      {'word': 'horse', 'info': 'large animal you can ride'},
      {'word': 'cow', 'info': 'large animal you can milk'},
      {'word': 'camel', 'info': 'large animal with humps'},
      {'word': 'sheep', 'info': 'small animal with wool'},
      {'word': 'goat', 'info': 'small animal with horns'},
    ];

    for (final row in rows) {
      await database.store.execute(
          "INSERT INTO _Word (word, info) VALUES (@word, @info)",
          substitutionValues: {
            "word": row['word'],
            "info": row['info'],
          });
    }
  }
}

But now when I try to apply the migration with

aqueduct db upgrade --connect postgres:password@localhost:5432/words

I get the error:

*** There was an error connecting to the database 'null:null@:0/null'. Reason: unable to connect to database.

I tracked this error message as coming from here in the source code.


Solution

  • It turned out to be a simple fix. I hadn't included the database user name in the CLI upgrade command.

    aqueduct db upgrade --connect postgres://words_user:password@localhost:5432/words
    

    I found it by comparing what I had written to the example in the documentation.

    I got a similar error another time when I forgot to use the right database name. And another time when I had a space before the password. Remember to include all the parts and make sure that everything is formatted correctly. The general format is

    aqueduct db upgrade --connect postgres://username:password@host:port/databaseName
    

    You can get help with the CLI command by typing

    aqueduct db --help