How to create Database and Tables in PostgreSQL

CREATE DATABASE creates a new PostgreSQL database.To create a database, you must be a superuser or have the special CREATEDB privilege. Below are the steps for creating a new Database and 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 ;

Categories: PostgreSQL