← Back to blog

Updated 18 November 2026

Migrating from Hosted Authentication to Postgres


Moving from a hosted authentication provider to Postgres changes which system owns user records, credentials, sessions, and account state. The safe migration is a controlled data and traffic cutover. Own Auth runs inside your application, reads its connection from DATABASE_URL, and writes authentication records to your database. Your application becomes responsible for operating that database and for deciding how existing provider identities map into the new schema.

Inventory the records that must move

Start with an export that identifies every user and every sign in method attached to that user. Preserve stable application identifiers when other tables reference them. Record normalized email addresses, verification state, names, profile fields, disabled state, organisation membership, roles, password hashes, and the provider subject used by each OAuth identity. Do not merge accounts only because two records share an email address. Establish an explicit conflict rule and review collisions before importing anything.

Treat credentials as a separate part of the migration. A password hash is useful only when you know its algorithm, parameters, encoding, and salt format. OAuth identities need the provider name and provider subject. Multifactor secrets and recovery codes require their own supported import path. If the source provider does not export a usable credential, mark that account for password reset or identity relinking instead of inventing a conversion.

Create the Own Auth schema and instance

Apply the schema from the installed Own Auth package to the target Postgres database. Configure the application with DATABASE_URL and OWN_AUTH_TOKEN_PEPPER, then create one shared Own Auth instance. Session lifetime uses ttlMs and idleTtlMs. Password policy uses supported fields such as minLength. Own Auth selects the default password hasher without a separate work factor or migration switch.

auth.ts
import { createOwnAuth } from "own-auth";

export const auth = createOwnAuth({
  tokenPepper: process.env.OWN_AUTH_TOKEN_PEPPER,
  baseUrl: "https://app.example.com",
  session: {
    ttlMs: 30 * 24 * 60 * 60 * 1000,
    idleTtlMs: 7 * 24 * 60 * 60 * 1000,
  },
  password: {
    minLength: 12,
  },
});

Keep this configuration in one module and call its public methods from your routes or service layer. Own Auth reads the Postgres connection from the environment by default. Passing an arbitrary pg.Pool as a database option is not part of the public configuration shown here.

Preserve password access without preserving weak hashes forever

Own Auth creates new password hashes with Argon2id by default. It can verify its legacy scrypt format. After a user supplies the correct password, Own Auth replaces that scrypt hash with the current Argon2id format before returning a completed session or a multifactor challenge. The upgrade is automatic and requires no migration switch.

That compatibility applies to the formats the installed package understands. It does not mean that every vendor hash can be copied into the database. Compare the provider export with the formats accepted by Own Auth before planning a transparent migration. Accounts with unsupported or unavailable hashes need a password reset flow. Never ask users for their existing password outside the normal sign in form, and never log a raw password during migration.

Replace provider sessions at the cutover

Hosted provider cookies and tokens should not become Own Auth sessions. They were signed, stored, or revoked under a different trust model. Choose a cutover time, stop creating new provider sessions, and require users to sign in through Own Auth. If a short overlap is required, keep both verification paths explicit and time limited. Do not silently accept an old provider token as proof for creating a new local session unless you have designed and audited that exchange.

A successful password sign in can also return mfa_required. The application must narrow the result before reading user, session, or sessionToken. When a challenge is returned, send only the challenge token, allowed methods, and expiry needed by the second factor screen.

sign-in.ts
const result = await auth.signInEmailPassword({ email, password });

if (result.status === "mfa_required") {
  return {
    status: result.status,
    challengeToken: result.challengeToken,
    methods: result.methods,
    expiresAt: result.expiresAt,
  };
}

return {
  status: result.status,
  user: {
    id: result.user.id,
    email: result.user.email,
    name: result.user.name,
  },
  session: { expiresAt: result.session.expiresAt },
  sessionToken: result.sessionToken,
};

Run the import as a repeatable operation

  1. Take a fresh provider export and record its generation time.
  2. Transform records into an explicit import format without changing the source export.
  3. Validate identifiers, required fields, identity collisions, credential formats, and organisation references.
  4. Import into a nonproduction database and compare record counts plus selected account histories.
  5. Rehearse sign in, password reset, email verification, OAuth linking, multifactor challenges, sign out, and session revocation.
  6. Schedule the final export, pause conflicting writes, import the delta, and switch application traffic.
  7. Keep the source system read only for the agreed recovery period, then remove its keys and callbacks.

Make the importer safe to rerun. Use stable source identifiers, reject ambiguous matches, and write a result for every record. A rerun should update the same intended account or report a conflict. It should not create a second account. Keep the import log free of password hashes, tokens, multifactor secrets, and other credential material.

Verify the result before removing the provider

Compare totals for users, identities, memberships, and credentials. Sample active, disabled, verified, unverified, password, OAuth, and multifactor accounts. Confirm that unsupported password hashes enter the reset path and that imported scrypt hashes are replaced after a successful sign in. Confirm that old sessions no longer authorize requests after the cutover and that new sessions can be revoked from the application database.

The migration is complete when the application uses Own Auth for every new authentication decision, the database contains the expected records, all supported account paths pass, and the hosted provider cannot create or validate another session. At that point, remove the provider SDK, callback routes, webhooks, secrets, and billing plan as one deliberate cleanup step.