PostgreSQL is a powerful, open-source object-relational database system with a strong reputation for reliability, feature robustness, and performance.
-- Create a new database
CREATE DATABASE mydatabase;
-- Create a new table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE NOT NULL
);
-- Insert data into the table
INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');
-- Query data from the table
SELECT * FROM users;
- Storing relational data
- Complex queries and transactions
- Data integrity and constraints
- Full-text search
- Indexes: Improve the speed of data retrieval.
- Views: Create virtual tables based on the result of a query.
- Stored Procedures: Write functions in SQL or other languages.
- Triggers: Execute a function in response to an event on a table.
-- Create an index on the email column
CREATE INDEX idx_users_email ON users(email);
-- Create a view for active users
CREATE VIEW active_users AS
SELECT * FROM users WHERE active = true;
-- Create a stored procedure to update user email
CREATE OR REPLACE FUNCTION update_user_email(user_id INT, new_email VARCHAR)
RETURNS VOID AS $$
BEGIN
UPDATE users SET email = new_email WHERE id = user_id;
END;
$$ LANGUAGE plpgsql;
-- Create a trigger to log changes to the users table
CREATE OR REPLACE FUNCTION log_user_changes()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO user_changes(user_id, change_time, change_type)
VALUES (NEW.id, NOW(), TG_OP);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER user_changes_trigger
AFTER INSERT OR UPDATE OR DELETE ON users
FOR EACH ROW EXECUTE FUNCTION log_user_changes();
- Use Indexes: Use indexes to speed up data retrieval.
- Normalize Data: Normalize your data to reduce redundancy and improve data integrity.
- Backup Regularly: Regularly backup your database to prevent data loss.
- Monitor Performance: Use tools to monitor and optimize database performance.
- Security: Use roles and permissions to secure your database.