copy mytable from '~/Downloads/dummy.csv' with delimiter ',' csv;
Creating and Restoring Dumps
In the regular shell:
Command
Description
$ pg_dump mydbname > myfile.sql
Dump a database to a file
$ psql mydbname < myfile.sql
Restore a database from a file
Dummy Database
-- Create the databaseCREATEDATABASEdummy_db;-- Connect to the database\cdummy_db;-- Create the users tableCREATETABLEusers(user_idSERIALPRIMARYKEY,usernameVARCHAR(50)NOTNULLUNIQUE,emailVARCHAR(100)NOTNULLUNIQUE,passwordVARCHAR(100)NOTNULL,created_atTIMESTAMPDEFAULTCURRENT_TIMESTAMP);-- Create the products tableCREATETABLEproducts(product_idSERIALPRIMARYKEY,nameVARCHAR(100)NOTNULL,descriptionTEXT,priceDECIMAL(10,2)NOTNULL,stock_quantityINTNOTNULLCHECK(stock_quantity>=0),created_atTIMESTAMPDEFAULTCURRENT_TIMESTAMP);-- Create the orders tableCREATETABLEorders(order_idSERIALPRIMARYKEY,user_idINTNOTNULLREFERENCESusers(user_id)ONDELETECASCADE,order_dateTIMESTAMPDEFAULTCURRENT_TIMESTAMP,total_amountDECIMAL(10,2)NOTNULL,statusVARCHAR(20)DEFAULT'Pending',CONSTRAINTvalid_total_amountCHECK(total_amount>=0));-- Create a junction table for order items (many-to-many relationship)CREATETABLEorder_items(order_item_idSERIALPRIMARYKEY,order_idINTNOTNULLREFERENCESorders(order_id)ONDELETECASCADE,product_idINTNOTNULLREFERENCESproducts(product_id)ONDELETECASCADE,quantityINTNOTNULLCHECK(quantity>0),priceDECIMAL(10,2)NOTNULLCHECK(price>=0),UNIQUE(order_id,product_id));-- Sample Index (optional)CREATEINDEXidx_users_usernameONusers(username);-- Sample Views (optional)CREATEVIEWuser_ordersASSELECTu.user_id,u.username,o.order_id,o.order_date,o.total_amount,o.statusFROMusersuJOINordersoONu.user_id=o.user_id;-- Insert dummy data into the users tableINSERTINTOusers(username,email,password)VALUES('john_doe','john@example.com','password123'),('jane_smith','jane@example.com','securepassword'),('mike_brown','mike@example.com','mypassword');-- Insert dummy data into the products tableINSERTINTOproducts(name,description,price,stock_quantity)VALUES('Laptop','High performance laptop with 16GB RAM and 512GB SSD',1200.00,50),('Smartphone','Latest model smartphone with amazing features',800.00,100),('Headphones','Wireless headphones with noise cancellation',150.00,200),('Desk Chair','Ergonomic desk chair for comfortable seating',300.00,20);-- Insert dummy data into the orders tableINSERTINTOorders(user_id,total_amount,status)VALUES(1,1500.00,'Completed'),(2,950.00,'Pending'),(3,300.00,'Shipped');-- Insert dummy data into the order_items tableINSERTINTOorder_items(order_id,product_id,quantity,price)VALUES(1,1,1,1200.00),-- John ordered 1 Laptop(1,3,2,150.00),-- John ordered 2 Headphones(2,2,1,800.00),-- Jane ordered 1 Smartphone(2,4,1,300.00),-- Jane ordered 1 Desk Chair(3,4,1,300.00);-- Mike ordered 1 Desk Chair
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)