Method EnsureDatabaseExistsAsync
EnsureDatabaseExistsAsync(string, string, CancellationToken)
Ensures that the PostgreSQL database named in the supplied connection string exists, creating it if it
does not. Because a connection cannot be opened to a database that does not yet exist, this method
connects to an existing maintenance database (maintenanceDatabase, postgres by
default) using the same host and credentials, and issues CREATE DATABASE there.
public static Task EnsureDatabaseExistsAsync(string connectionString, string maintenanceDatabase = "postgres", CancellationToken cancellationToken = default)
Parameters
connectionStringstringA connection string whose
Databasevalue identifies the database to ensure exists. The same host, port, and credentials are reused to reach the maintenance database.maintenanceDatabasestringAn existing database to connect to in order to issue
CREATE DATABASE. Defaults topostgres.cancellationTokenCancellationTokenAn optional token to cancel the operation.
Returns
- Task
A task that completes once the target database exists.
Remarks
The connecting role must have the CREATEDB privilege and access to the maintenance database.
This method only creates the database; it does not install the vector extension (that is a
server-side installation) nor create any collections.
Exceptions
- ArgumentException
Thrown when the connection string is null/empty or specifies no database.