DEV Community

caglarcercinlidev
caglarcercinlidev

Posted on

2 2

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay