How to modify table in PostgreSQL

Below are the steps for creating a new Database, Table and modifying an existing table in PostgreSQL.

  • Switch to postrges user.
  • >> sudo su - postgres
  • Switch to psql — PostgreSQL interactive terminal
  • >> psql
  • Create new database and list databases
  • >> CREATE DATABASE test_db;
    \l ;
  • Switch to test_db
  • >> \c test_db ;
  • Create table in test_db and list tables
  • >> CREATE TABLE test_table (id integer, name varchar(40) UNIQUE);
    >> \dt ;
  • Modify table with adding new column and verify schema.
  • >> ALTER TABLE test_table ADD COLUMN age integer;
    >> select * from test_table;


Category: PostgreSQL