Sunday, March 27, 2016

basic postgresql operation

1- installation
$ sudo apt-get install postgresql

2- login into postgresql
$ sudo -u postgres psql

3- to create database
postgres=# CREATE DATABASE ptest;
CREATE DATABASE


3- list databases
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 ptest     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)


4- use a database or connect to a database
postgres=# \c ptest
You are now connected to database "ptest" as user "postgres".

5- create table
ptest=# CREATE TABLE users (
     id    integer PRIMARY KEY,
     sex   varchar(10) NOT NULL,
     name   varchar(40) NOT NULL
);
CREATE TABLE


6- list tables
ptest(# \dt
         List of relations
 Schema | Name  | Type  |  Owner  
--------+-------+-------+----------
 public | users | table | postgres
(1 row)


7- insert record into table
ptest=# insert into users values('1','male', 'ptesta');
INSERT 0 1


8- query record from table
ptest=# select * from users;
 id | sex  |  name 
----+------+--------
  1 | male | ptesta
(1 row)


9 - exit from postgresql shell
ptest=# \q

No comments:

Post a Comment