PostgreSQL DDL
PostgreSQL-compatible TiDB Cloud Starter supports common PostgreSQL Data Definition Language (DDL) statements for creating and managing database objects.
Create a table
Use CREATE TABLE to create a table:
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE,
active BOOLEAN DEFAULT true,
created_at TIMESTAMPTZ DEFAULT now()
);
The following common constraints and column properties are supported:
NOT NULLDEFAULTPRIMARY KEYUNIQUEREFERENCESandFOREIGN KEYCHECK
For example:
CREATE TABLE orders (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(id),
amount NUMERIC(10, 2) CHECK (amount >= 0)
);
Foreign keys support the following common ON DELETE and ON UPDATE actions:
CASCADESET NULLSET DEFAULTRESTRICTNO ACTION
Create a table from a query
CREATE TABLE ... AS SELECT and SELECT ... INTO are supported:
CREATE TABLE active_users_snapshot AS
SELECT id, name, email
FROM users
WHERE active = true;
SELECT id, name
INTO user_names
FROM users;
Identity columns
GENERATED ALWAYS AS IDENTITY and GENERATED BY DEFAULT AS IDENTITY are supported:
CREATE TABLE items (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE tasks (
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title TEXT NOT NULL
);
Identity columns have compatibility differences for sequence options such as custom starting values and increments. If you need a custom starting value or increment, use an explicit sequence.
For example:
CREATE SEQUENCE order_seq
START WITH 1000
INCREMENT BY 5;
CREATE TABLE sequence_orders (
id BIGINT PRIMARY KEY DEFAULT nextval('order_seq'),
description TEXT
);
Generated columns
CREATE TABLE products (
id BIGSERIAL PRIMARY KEY,
price NUMERIC,
tax NUMERIC,
total NUMERIC GENERATED ALWAYS AS (price + tax) STORED
);
Create an index
B-tree is the default index access method:
CREATE INDEX idx_users_name
ON users (name);
Create a unique index:
CREATE UNIQUE INDEX idx_users_email
ON users (email);
Expression indexes are supported:
CREATE INDEX idx_users_lower_email
ON users (lower(email));
Partial indexes are supported:
CREATE INDEX idx_active_users
ON users (id)
WHERE active = true;
CREATE INDEX CONCURRENTLY is also supported:
CREATE INDEX CONCURRENTLY idx_users_created_at
ON users (created_at);
The following index access methods are supported:
For HNSW index requirements, see Vector Search.
For unsupported PostgreSQL index access methods, see PostgreSQL Compatibility.
GIN index example
Create a GIN index on a JSONB column:
CREATE TABLE documents (
id BIGSERIAL PRIMARY KEY,
metadata JSONB
);
CREATE INDEX idx_documents_metadata
ON documents
USING GIN (metadata);
Then query JSONB containment:
SELECT *
FROM documents
WHERE metadata @> '{"type":"pdf"}';
GIN indexes can also be used for full-text search. For more information, see Full-Text Search.
Create and manage views
Create a view:
CREATE VIEW active_users AS
SELECT id, name, email
FROM users
WHERE active = true;
Replace an existing view:
CREATE OR REPLACE VIEW active_users AS
SELECT id, name, email, created_at
FROM users
WHERE active = true;
CREATE OR REPLACE VIEW can append columns, but it cannot drop or rename existing view columns.
Create and refresh a materialized view:
CREATE MATERIALIZED VIEW user_summary AS
SELECT active, count(*) AS user_count
FROM users
GROUP BY active;
REFRESH MATERIALIZED VIEW user_summary;
Alter a table
The following common ALTER TABLE operations are supported:
Changing the type of a primary-key column or the referencing column of a foreign key is not supported. For more information, see PostgreSQL Compatibility.
Drop database objects
Use the following statements to drop database objects:
DROP TABLE IF EXISTS users;
DROP INDEX IF EXISTS idx_users_name;
DROP VIEW IF EXISTS active_users;
DROP MATERIALIZED VIEW IF EXISTS user_summary;
CASCADE can be used to drop dependent objects such as views and materialized views, but foreign-key dependencies must be handled separately:
DROP TABLE users CASCADE;
To avoid leaving dangling foreign-key constraints, remove any foreign-key constraints that reference a table before dropping the table. Foreign-key dependency handling differs from PostgreSQL. For more information, see PostgreSQL Compatibility.
Truncate a table
Use TRUNCATE or TRUNCATE TABLE for a single table:
TRUNCATE TABLE users;
The following PostgreSQL TRUNCATE forms are not supported:
- Multi-table
TRUNCATE CASCADERESTRICTONLYRESTART IDENTITYCONTINUE IDENTITY
A table referenced by a foreign key cannot be truncated.
Create and manage schemas
Create a schema:
CREATE SCHEMA app;
Create an object in that schema:
CREATE TABLE app.settings (
key TEXT PRIMARY KEY,
value TEXT
);
Drop the schema when it is no longer needed:
DROP SCHEMA app CASCADE;
Create and manage sequences
Create a sequence:
CREATE SEQUENCE invoice_seq
START WITH 1000
INCREMENT BY 1;
Use the sequence:
SELECT nextval('invoice_seq');
Use setval() to reposition a sequence:
SELECT setval('invoice_seq', 2000);
ALTER SEQUENCE supports ownership operations such as OWNER TO and OWNED BY. Other PostgreSQL ALTER SEQUENCE options are not supported. For more information, see PostgreSQL Compatibility.
Other DDL statements
The following common DDL statements are also supported: