CREATE Statements

The CREATE statements are used to declaratively create tables, views, functions etc. If you went through the psql section of the tutorial, you might have executed some of these commands:

Example of PostgreSQL CREATE DATABASE

In order to create a new database in Postgres, you can run the following statement.

CREATE DATABASE myapp;

We can verify the above command by executing the following in psql interface.

\l

Example of PostgreSQL CREATE TABLE

Next up, to create a new table, we can follow the syntax of CREATE TABLE <table_name>(column_name data_type constraints);

For example:

CREATE TABLE users(
id integer,
name text,
age integer,
is_active boolean
);

Once the table is created, you can verify this using psql by executing the following command:

\dt

It will return something like this:

List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | users | table | postgres
(1 row)

Note: By default, all the statements are executed against the public schema.

Example of PostgreSQL CREATE USER

CREATE USER praveen

This would create a user called praveen. You can also assign the user a role while creating.

CREATE USER praveen with SUPERUSER;

The above command gives the user super user privilege.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin