DEV Community

caglarcercinlidev
caglarcercinlidev

Posted on

HOW TO RUN A SQL FILE AND CREATE A DATABASE

It is not always a good practice to insert data with a psql command line. Creating a sql file and running it is sometimes easier.

First we create a file with .sql extension (with notepad for example).

DROP TABLE products;

create table products (id serial primary key, name varchar (50) not null, price varchar (50) not null);
 insert into products (name, price) values ('hamburger','10');
Enter fullscreen mode Exit fullscreen mode

Why DROP TABLE?
It would delete the previous table and rewrite it, otherwise it would cause en error.
Then we log into psql.

run \i fastfoodshop.sql

It is important to indicate the correct place of the file.

After that, check table and entries.

Wish you an easy coding!

Top comments (0)