All rules

Safety

postgresql/consistent-create-or-replace

Enforce a consistent stance on `CREATE OR REPLACE` for `FUNCTION` / `PROCEDURE` / `VIEW`.

  • Type suggestion
  • Recommended off
  • Fixable yes

Why this matters

Two valid stances exist: (a) always use `CREATE OR REPLACE` so re-running a migration is idempotent, or (b) never use it so unintended overwrites are surfaced as `relation already exists`. Pick one with the `style` option; the rule auto-fixes either way. `CREATE TABLE` / `CREATE INDEX` are out of scope (they don't support `OR REPLACE`).

Examples

Incorrect

Incorrect
CREATE FUNCTION fn() RETURNS void LANGUAGE SQL AS '';
Incorrect
CREATE PROCEDURE p() LANGUAGE SQL AS '';
Incorrect
CREATE VIEW v AS SELECT 1;

Correct

Correct
CREATE OR REPLACE FUNCTION fn() RETURNS void LANGUAGE SQL AS '';
Correct
CREATE OR REPLACE PROCEDURE p() LANGUAGE SQL AS '';
Correct
CREATE OR REPLACE VIEW v AS SELECT 1;

Configure it

// eslint.config.js
import postgresql from "eslint-plugin-postgresql";

export default [
  {
    files: ["**/*.sql"],
    languageOptions: {
      parser: postgresql.configs.recommended.languageOptions.parser,
    },
    plugins: { postgresql },
    rules: {
      "postgresql/consistent-create-or-replace": [
        "warn",
        {
          style: "always",
        },
      ],
    },
  },
];

Options

style "always" | "never" default: "always"
Which stance to enforce. `always` (default) requires `OR REPLACE` so re-running is idempotent. `never` forbids `OR REPLACE` so unintended overwrites are surfaced.
Try this rule

Edit the SQL — only consistent-create-or-replace is enabled.

Pre-filled with the first incorrect example. Toggle off in the rule shelf to see how the diagnostic disappears.

0 errors 0 warnings parse 0ms · rules 0ms
Diagnostics

No issues found.

2 rules enabled.

Rule under test consistent-create-or-replace — plus no-syntax-error as a safety net.
eslint-plugin-postgresql

An ESLint plugin that lints .sql files with real PostgreSQL grammar and a curated set of best-practice rules.

© 2026 eslint-plugin-postgresql contributors Built on libpg-query · PostgreSQL 17