Saturday 18 May 2019

Postgres: Create table

'CREATE TABLE' statement creates a table.

 

Example

CREATE TABLE employee (id INT, name VARCHAR(20));

 

 
Let's create a new database and create employee table in it.


Create a database

Execute below command to create database.

createdb -U postgres -h localhost {databaseName}

 

Example

createdb -U postgres -h localhost myOrg

 

Once the database is created, you can login to the database by executing the command ‘psql --dbname=myOrg’.

$createdb -U postgres -h localhost myOrg
Password:

Login to myOrg database

psql -U postgres -h localhost --dbname=myOrg


$psql -U postgres -h localhost --dbname=myOrg
Password for user postgres: 
psql (14.4)
Type "help" for help.

myOrg=#

Create employee table and query

myOrg=# CREATE TABLE employee (id INT, name VARCHAR(20));
CREATE TABLE
myOrg=# 
myOrg=# SELECT * FROM employee;
 id | name 
----+------
(0 rows)
Previous                                                 Next                                                 Home

No comments:

Post a Comment