How to Create A Table In PostgreSQL?

6 minutes read

To create a table in PostgreSQL, you can use the CREATE TABLE statement. This statement allows you to define the table's structure, including its columns and data types.


The basic syntax for creating a table is as follows:


CREATE TABLE table_name ( column1 datatype1, column2 datatype2, column3 datatype3, ... );


Here, table_name is the name you want to give to your table. Inside the parentheses, you list the columns you want the table to have, along with their respective data types. Each column-definition takes the form column_name datatype.


For example, let's say we want to create a table named "employees" with columns for "id", "name", and "salary". We can write the following:


CREATE TABLE employees ( id INT, name VARCHAR(50), salary DECIMAL(10,2) );


In this example, the "id" column has the data type INT (integer), the "name" column has VARCHAR(50) type (variable-length string with a maximum length of 50 characters), and the "salary" column has DECIMAL(10,2) type (decimal number with 10 digits and 2 decimal places).


After executing the CREATE TABLE statement, PostgreSQL will create the table with the specified columns and data types. You can now insert data into this table using the INSERT INTO statement or perform various operations such as updating, deleting, and querying data.

Best Managed PostgreSQL Providers of December 2024

1
DigitalOcean

Rating is 5 out of 5

DigitalOcean

2
Vultr

Rating is 5 out of 5

Vultr

3
AWS

Rating is 5 out of 5

AWS

4
Cloudways

Rating is 4.9 out of 5

Cloudways


What is the syntax to create a table in PostgreSQL?

The syntax to create a table in PostgreSQL is as follows:

1
2
3
4
5
6
CREATE TABLE table_name (
    column1 datatype constraint,
    column2 datatype constraint,
    ...
    columnN datatype constraint
);


Here, table_name is the name of the table you want to create, and column1, column2, ..., columnN are the names of the columns in the table. datatype represents the data type of each column, such as integer, text, date, etc. And constraint represents any constraints or rules you want to define for each column, such as NULL or NOT NULL, PRIMARY KEY, FOREIGN KEY, etc.


Here's an example of creating a table called "employees" with columns "id", "name", and "salary":

1
2
3
4
5
CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    salary DECIMAL(10,2)
);


In this example, the id column is defined as a primary key using the SERIAL data type, which is an auto-incrementing integer. The name column is defined as a mandatory VARCHAR data type with a maximum length of 50 characters. The salary column is defined as a DECIMAL data type with a precision of 10 and a scale of 2, allowing for a maximum of 8 digits before the decimal point and 2 digits after the decimal point.


How to create a table with a column of type money in PostgreSQL?

To create a table with a column of type money in PostgreSQL, you can use the following syntax:

1
2
3
CREATE TABLE table_name (
   column_name money
);


Here, table_name is the name of your table and column_name is the name of your column.


For example, let's say you want to create a table named "products" with a column of type money named "price". You can use the following SQL statement:

1
2
3
CREATE TABLE products (
   price money
);


This will create a table named "products" with a column named "price" of type money.


How to create a table with a column of type array in PostgreSQL?

To create a table with a column of type array in PostgreSQL, you can use the CREATE TABLE command with the ARRAY data type. Here's an example:

1
2
3
4
5
6
CREATE TABLE your_table_name (
    id SERIAL PRIMARY KEY,
    your_array_column_name data_type[],
    other_columns data_type,
    ...
);


In the example above, your_table_name is the name of your table, your_array_column_name is the name of the column that will store an array, and data_type is the data type of the elements in the array.


For instance, if you want to create a table named users with a column named skills storing an array of text values, you could use the following SQL statement:

1
2
3
4
5
6
7
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    skills text[],
    name text NOT NULL,
    email text NOT NULL,
    ...
);


This users table will have a primary key column id, an array column skills to store the skills of each user, and other columns like name and email.


Remember to adjust the table name, column names, data types, and other columns according to your specific requirements.


What is the purpose of the primary key constraint in PostgreSQL table creation?

The purpose of the primary key constraint in PostgreSQL table creation is to uniquely identify each record or row in the table. A primary key constraint ensures that the values in the specified column(s) are unique and not null. It also helps enforce data integrity and provides a quick way to reference and access specific records in the table.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To set a table with IGNORE_DUP_KEY on PostgreSQL, you would need to utilize the ON CONFLICT clause when creating the table or altering an existing table. This clause allows you to specify how PostgreSQL should handle duplicate key violations when inserting dat...
To create a temporary table in PostgreSQL, you can use the "CREATE TEMPORARY TABLE" command. Temporary tables are only visible to the current session and are automatically dropped at the end of the session or transaction. Here's the syntax to creat...
To import data from a CSV file into a PostgreSQL table, you can follow these steps:Make sure you have PostgreSQL installed and running on your system. Create a new table in your PostgreSQL database that matches the structure of the CSV file. Define the column ...