All rules

Style

postgresql/prefer-cast-operator

Enforce a single style for type casts (`x::int` or `CAST(x AS int)`).

  • Type layout
  • Recommended off
  • Fixable yes

Why this matters

PostgreSQL accepts both forms; this rule picks one and rewrites the other. Default is the operator form `x::int`. Switch with the `form` option.

Examples

Incorrect

Incorrect
SELECT CAST(x AS integer) FROM t;
Incorrect
SELECT CAST(x AS varchar(255)) FROM t;

Correct

Correct
SELECT x::integer FROM t;
Correct
SELECT x::numeric(10, 2) FROM t;

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/prefer-cast-operator": [
        "warn",
        {
          form: "operator",
        },
      ],
    },
  },
];

Options

form "operator" | "function" default: "operator"
Which form of cast to enforce. `operator` rewrites `CAST(x AS T)` to `x::T`; `function` rewrites the other direction.
Try this rule

Edit the SQL — only prefer-cast-operator 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 prefer-cast-operator — 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