Basic CRUD Operations in PostgreSQL Example

Last Updated On Sunday 19th Dec 2021
  • To write a string inside PostgreSQL you should use single quotes.
  • To write a list use (). Separate values with commas.

Create Table

	CREATE TABLE <table_name> (
  id SERIAL PRIMARY KEY,
  username VARCHAR(30) NOT NULL,
  email VARCHAR(40),
  password VARCHAR(50),
  phone VARCHAR(25)
  created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  CHECK (COALESCE(email, phone) IS NOT NULL
);
	

Delete Table

	DROP <table_name>;
	

Insert data into table

	INSERT INTO <table_name> (username, email, password, phone)
VALUES ('my_username', 'email@email.com', 'password', '123456789');
	
  • To insert multiple rows, wrap the content inside value in () and separate each row entry with a comma.

Update data from table

	UPDATE <table_name> SET <column_name> = <value> WHERE <select_row_to_update>;
	

Delete data from table

	DELETE FROM <table_name> WHERE <condition_to_pick_row_to_delete>;