A PostgreSQL database.
+Creates a new Catalog.
-Optionallocale: stringThe default encoding for this database
-Formats this object for use in a GRANT statement.
-This object name for use in a GRANT statement
-The default locale for this database
-Gets the name of this database object.
-The name of this database object
-Assigns privileges for this object to a role.
-The role to receive the privileges
-The privileges to grant
-The grant statement
-Produces a SQL statement that creates this object.
-The SQL statement
-Creates a new Catalog.
+Optionallocale: stringThe default encoding for this database
+Formats this object for use in a GRANT statement.
+This object name for use in a GRANT statement
+The default locale for this database
+Gets the name of this database object.
+The name of this database object
+Assigns privileges for this object to a role.
+The role to receive the privileges
+The privileges to grant
+The grant statement
+Produces a SQL statement that creates this object.
+The SQL statement
+A connection to a named database on a PostegreSQL server.
-A connection to a named database on a PostegreSQL server.
+Creates a new DatabaseClient.
-The pg Client instance
-Configures roles and privileges for admin usage of a schema.
-A Promise that resolves to the new grants and default privileges
-Configures roles and privileges for read-only usage of a schema.
-A Promise that resolves to the new grants and default privileges
-Creates a new DatabaseClient.
+The pg Client instance
+Configures privileges for admin usage of a schema.
+The connected user must be the schema owner. This method doesn't grant +membership in the admin role; use ServerClient.createMemberships +for that.
+A Promise that resolves to the new grants and default privileges
+Configures privileges for read-only usage of a schema.
+The connected user must be the schema owner. This method doesn't grant +membership in the reader role; use ServerClient.createMemberships +for that.
+A Promise that resolves to the new grants and default privileges
+A modification to the default privileges used when objects are created.
-A modification to the default privileges used when objects are created.
+Creates a new AlterDefaultPrivileges.
-The privileges to define
-Optionalcreator: RoleOptional, the role doing the creating
-Optionalschema: SchemaOptional, The schema in which the privileges apply
-The role whose creations use the default privileges
-The default privileges applied
-The schema in which the privileges apply
-Constrains these defaults to a specific creator.
-The role whose creations use the default privileges
-A copy of this object, constrained to the given creator
-Constrains these defaults to a specific schema.
-The schema in which the privileges apply
-A copy of this object, constrained to the given schema
-Produces a SQL statement that creates this object.
-The SQL statement
-Creates a new AlterDefaultPrivileges.
+The privileges to define
+Optionalcreator: Role
+Optional, the role doing the creating
+Optionalschema: Schema
+Optional, The schema in which the privileges apply
+The role whose creations use the default privileges
+The default privileges applied
+The schema in which the privileges apply
+Constrains these defaults to a specific creator.
+The role whose creations use the default privileges
+A copy of this object, constrained to the given creator
+Constrains these defaults to a specific schema.
+The schema in which the privileges apply
+A copy of this object, constrained to the given schema
+Produces a SQL statement that creates this object.
+The SQL statement
+A definition of access privileges.
-A definition of access privileges.
+A PostgreSQL user login.
-A PostgreSQL user login.
+Creates a new Login.
-The login username
-The login password
-Gets the name of this database object.
-The name of this database object
-Gets the login password.
-The login password
-An alias for .name
-The username
-Assigns this role to another role (or login).
-The grantee
-The new Membership object
-Creates a new Login.
+The login username
+The login password
+Gets the name of this database object.
+The name of this database object
+Gets the login password.
+The login password
+An alias for .name
+The username
+Assigns this role to another role (or login).
+The grantee
+The new Membership object
+An assignment of a role to another role.
-An assignment of a role to another role.
+Creates a new Membership.
-Gets the role or privilege given to the grantee.
-The grant entitlement
-Creates a new Membership.
+Gets the role or privilege given to the grantee.
+The grant entitlement
+AbstractA named database object.
-AbstractA named database object.
+An assignment of privileges on a securable object (e.g. schema, table).
-An assignment of privileges on a securable object (e.g. schema, table).
+Creates a new Privileges object.
-The role to receive the privileges
-The named resource for which the privileges apply
-The privileges to grant
-Gets the role or privilege given to the grantee.
-The grant entitlement
-The privileges being granted
-The named database object for which the privileges apply
-Creates a new Privileges object.
+The role to receive the privileges
+The named resource for which the privileges apply
+The privileges to grant
+Gets the role or privilege given to the grantee.
+The grant entitlement
+The privileges being granted
+The named database object for which the privileges apply
+A PostgreSQL role.
-A PostgreSQL role.
+Gets the name of this database object.
-The name of this database object
-Assigns this role to another role (or login).
-The grantee
-The new Membership object
-Gets the name of this database object.
+The name of this database object
+Assigns this role to another role (or login).
+The grantee
+The new Membership object
+A PostgreSQL schema.
-A PostgreSQL schema.
+The catalog to which this schema belongs.
-Formats this object for use in a GRANT statement.
-This object name for use in a GRANT statement
-Gets the name of this database object.
-The name of this database object
-The owner of this schema, or undefined
-A Securable that represents all routines currently in this schema
-A Securable that represents all sequences currently in this schema
-A Securable that represents all tables currently in this schema
-Changes the owner of this schema to someone else.
-The new owner
-An executable statement
-Assigns privileges for this object to a role.
-The role to receive the privileges
-The privileges to grant
-The grant statement
-Sets the default privileges for newly created sequences in this schema.
-The role to receive the privileges
-The privileges to grant
-Sets the default privileges for newly created sequences in this schema.
-The role to receive the privileges
-The privileges to grant
-Sets the default privileges for newly created tables in this schema.
-The role to receive the privileges
-The privileges to grant
-Produces a SQL statement that creates this object.
-The SQL statement
-The catalog to which this schema belongs.
+Formats this object for use in a GRANT statement.
+This object name for use in a GRANT statement
+Gets the name of this database object.
+The name of this database object
+The owner of this schema, or undefined
+A Securable that represents all routines currently in this schema
+A Securable that represents all sequences currently in this schema
+A Securable that represents all tables currently in this schema
+Changes the owner of this schema to someone else.
+The new owner
+An executable statement
+Assigns privileges for this object to a role.
+The role to receive the privileges
+The privileges to grant
+The grant statement
+Sets the default privileges for newly created sequences in this schema.
+The role to receive the privileges
+The privileges to grant
+Sets the default privileges for newly created sequences in this schema.
+The role to receive the privileges
+The privileges to grant
+Sets the default privileges for newly created tables in this schema.
+The role to receive the privileges
+The privileges to grant
+Produces a SQL statement that creates this object.
+The SQL statement
+A database object that supports privileges.
-A database object that supports privileges.
+Creates a new Securable.
-A prefix for the object (e.g. "SCHEMA")
-The object name
-Formats this object for use in a GRANT statement.
-This object name for use in a GRANT statement
-Gets the name of this database object.
-The name of this database object
-Assigns privileges for this object to a role.
-The role to receive the privileges
-The privileges to grant
-The grant statement
-Creates a new Securable.
+A prefix for the object (e.g. "SCHEMA")
+The object name
+Formats this object for use in a GRANT statement.
+This object name for use in a GRANT statement
+Gets the name of this database object.
+The name of this database object
+Assigns privileges for this object to a role.
+The role to receive the privileges
+The privileges to grant
+The grant statement
+A connection to the "postgres" database on a PostegreSQL server.
-A connection to the "postgres" database on a PostegreSQL server.
+Create a new database catalog only if it does not already exist.
-The database catalog to create
-A Promise that resolves when the operation completes
-Execute a grant statement.
-The grant to perform
-A Promise that resolves when the operation completes
-Create a new login only if it does not already exist.
-The login to create
-A Promise that resolves when the operation completes
-Tests to see if the database already exists.
-The database name
-A Promise that resolves to true if the database exists
-Tests to see if the user already exists.
-The username to test
-A Promise that resolves to true if the user exists
-Creates a new ServerClient.
+The pg Client instance
+Create a new database catalog only if it does not already exist.
+The database catalog to create
+A Promise that resolves when the operation completes
+Execute a grant statement.
+The grant to perform
+A Promise that resolves when the operation completes
+Grants membership in a role to each of the given roles.
+Role memberships apply to the entire server, not to a single database. The
+connected user must have ADMIN OPTION on the role, such as the
+CREATEROLE user that created it.
A Promise that resolves to the new memberships
+Create a new login only if it does not already exist.
+The login to create
+A Promise that resolves when the operation completes
+Tests to see if the database already exists.
+The database name
+A Promise that resolves to true if the database exists
+Tests to see if the user already exists.
+The username to test
+A Promise that resolves to true if the user exists
+Provision databases and schemas in PostgreSQL along with roles, logins, and grants.
@@ -37,4 +47,40 @@Because all identifiers are quoted, that means the objects will be created using the same character casing as provided. Without double quotes, PostgreSQL creates objects with lowercase identifiers.
-ServerClient and DatabaseClient run these statements against a server. They don't require a superuser, which makes them suitable for managed services like Amazon RDS. Each runs as a different user:
ServerClient connects to the postgres database as a user with CREATEROLE (and CREATEDB, to create databases). It creates logins, roles, and databases, and grants role memberships.DatabaseClient connects to the new database as the schema owner. It grants privileges on the schema.import { DatabaseClient, ServerClient } from "@libreworks/db-provision-pgsql";
// As the CREATEROLE user, connected to the "postgres" database.
const server = new ServerClient(adminConnection);
await server.createMemberships(admin, [adminLogin]);
await server.createMemberships(readers, [readerLogin]);
// As the schema owner, connected to the new database.
const database = new DatabaseClient(ownerConnection);
await database.createAdminGrants(schema, admin);
await database.createReaderGrants(schema, readers);
+
+
+An object capable of producing a SQL statement.
+
A PostgreSQL database.
-