Introduction
Database security keeps your data safe from bad people and mistakes. It helps protect important information so only the right people can see or change it.
Jump into concepts and practice - no test required
Database security keeps your data safe from bad people and mistakes. It helps protect important information so only the right people can see or change it.
-- No specific code for this concept, but security uses commands like GRANT and REVOKE -- Example: GRANT SELECT ON table_name TO user_name; REVOKE INSERT ON table_name FROM user_name;
GRANT SELECT ON employees TO alice;
REVOKE UPDATE ON employees FROM bob;
This example creates a table, adds data, and gives a user only read access. It shows how security controls what users can do.
CREATE TABLE customers ( id SERIAL PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) ); INSERT INTO customers (name, email) VALUES ('John Doe', 'john@example.com'), ('Jane Smith', 'jane@example.com'); -- Give user 'guest' permission to only read data GRANT SELECT ON customers TO guest; -- Check what 'guest' can do (simulate by trying to insert) -- This will fail if run as 'guest': -- INSERT INTO customers (name, email) VALUES ('Eve', 'eve@example.com'); -- But 'guest' can run: SELECT * FROM customers;
Always give users the least access they need to do their job.
Use strong passwords and keep your database software updated.
Regularly check who has access and remove permissions if not needed.
Database security protects your data from unauthorized access.
Use commands like GRANT and REVOKE to control user permissions.
Keeping data safe helps prevent problems and follows privacy rules.
CREATE TABLE employees(id SERIAL PRIMARY KEY, name TEXT);GRANT SELECT ON employees TO guest_user;guest_user tries to run INSERT INTO employees(name) VALUES('Alice');?REVOKE SELECT ON employees FROM guest_user;