How To Create-Insert-Trancate-Drop Database And Table On Postgresql 9.6




Ok in here i want to tell you and to teach you, how can do create database and table until insert data in table and then truncate data table until drop your database. Ok i will teach you step by step.

Now you must be login your pc have postgres, and if you still in root, you must move to user postgres with command "su - postgres", ok follow command:

dba@dba-rizky:~$ ssh root@x.x.x.x
root@x.x.x.x's password:
Last login: Thu Apr 19 16:54:32 2018 from dba
[root@prod-odgpostgres ~]#su – postgres

And in here you can follow key "psql", ok follow command:

[postgres@prod-odgpostgres ~]$ psql

So, show your list database with command (slash)+l

postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------------+----------+----------+-------------+-------------+-----------------------
postgres | 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
(3 rows)

And then in here, now just have database default postgres not have your database build, how can do build new database? Ok follow my query
Example : create database [newdatabase];

postgres=#create database “ABCSYARIAH_DOLPINS”;
CREATE DATABASE

you can show again list database with command (slash) + l.

postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------------+----------+----------+-------------+-------------+-----------------------
ABCSYARIAH_DOLPINS | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | 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)

After create and show list database, you can used your new database with command (slash+c → name database).
Example :\c [namedatabase];

postgres=#\c ABCSYARIAH_DOLPINS;

Now, you can do that create new table because in your new database not have table (empty), so if you want create ne table you must be follow my query.
Example : create table [newtable] (namefield1 [type][primary], namefield2 [type] ,... etc);

postgres=#create table company (id int primary key not null, name text not null, age int not null, address char(50), salary real);

And the next, you can do input query insert because if you want show data in table, you must have data in your table, how you can show data in table? but you have not data in your table, its impossible. So follow my query in here.
Example : insert into [nametable] values ([datafield]);

postgres=# insert into company (id,name,age,address,salary) VALUES (1, 'Rizky', 21, 'Depok', 9999999);

Now, show all your data in table with query select, if you keyword “*” is information all data field in your table.
Example : select * from [nametable];

postgres=# select * from company;

But if you want select by data field you must be specific query, you must be follow query here.
Example : select [datafield] from [nametable];

postgres=# select name,address,salary from company where id=1;

And the next in here I want clean all data in table along with the table using query truncate, follow query in here.
Example : truncate table [nametable];

postgres=#truncate table company;

After finished truncate data, in here I want to drop my database “ABCSYARIAH_DOLPINS”, I want to using query “drop database” and show list database, whether the database we have done drop successfully erased. You can be follow query.
Example : drop database [namadatabaseygdibuat];

postgres=#drop database ABCSYARIAH_DOLPINS;
postgres=#\l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------------+----------+----------+-------------+-------------+-----------------------
postgres | 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
(3 rows)

Ok thank you for reading and see my tutorial.

Comments