Why this matters
Tables without a primary key cannot be replicated cleanly with logical replication, are hard to shard, and break most ORMs and migration tools. Partitions (`CREATE TABLE ... PARTITION OF ...`) inherit their parent's primary key and are not flagged.
Examples
Incorrect
CREATE TABLE t (id INT, name TEXT);Correct
CREATE TABLE t (id INT PRIMARY KEY, name TEXT);CREATE TABLE t (id INT, name TEXT, PRIMARY KEY (id));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/require-primary-key": "warn",
},
},
]; Options
Edit the SQL — only require-primary-key is enabled.
Pre-filled with the first incorrect example. Toggle off in the rule shelf to see how the diagnostic disappears.
Diagnostics
No issues found.
2 rules enabled.
Rule under test
require-primary-key — plus no-syntax-error as a safety net.