DEV Community

vidhya murali
vidhya murali

Posted on

SQL Challenge: One Concept Every Day

Day-1

Creating Databases and Tables in PostgreSQL

How to Create a Database in PostgreSQL

There are two common ways to create a PostgreSQL database:

  • Using Terminal / psql [Linux]
  • Using pgAdmin

Create a Database Using Terminal

sudo -i -u postgres
Enter fullscreen mode Exit fullscreen mode

sudo (superUser Do) → run a command with administrator privileges
-i → start a login shell
-u → specify which user to switch to
postgres → PostgreSQL system user

CREATE DATABASE company;
Enter fullscreen mode Exit fullscreen mode

we can check the version with the SQL statement ,

SELECT version();
Enter fullscreen mode Exit fullscreen mode

note :
\l - to Check databases

\c company - to connect Database

\d - It shows the tables, views, sequences, etc.
in the current database

In pgAdmin4

PostgreSQL Create Table

CREATE TABLE employee(
  id INT,
  name VARCHAR(25),
  role VARCHAR(25),
  exp INT
);
Enter fullscreen mode Exit fullscreen mode

PostgreSQL Insert Data

  1. Insert one row
INSERT INTO employee (id, name, role, experience)
VALUES (1, 'vidhya', 'Full Stack Developer', 1);
Enter fullscreen mode Exit fullscreen mode
  1. Insert multiple rows
INSERT INTO employee (id, name, role, experience)
VALUES
(1, 'Vidhya', 'Developer', 2),
(2, 'Arav', 'Tester', 3),
(3, 'Amutha', 'Manager', 5),
(4, 'Prem', 'Developer', 4);
Enter fullscreen mode Exit fullscreen mode

Display Table

To check the result we can display the table with this SQL statement:

SELECT * FROM employee;
Enter fullscreen mode Exit fullscreen mode

That's it for Day 1. To be continued.....

Top comments (0)