How to create a table in PostgreSQL

In this PostgreSQL, we will discuss How to Create a Table In PostgreSQL [Terminal + pgAdmin], for terminal psql which is an interactive terminal for interacting with the PostgreSQL database. Also, use the pgAdmin for creating the table which is the central hub for database management tasks.

How to Create a Table In PostgreSQL

Let us discuss all the approaches individually.

Approach-1 Using Terminal

To create a table in PostgreSQL, follow the below steps.

1. Open the command prompt or terminal.

how to create a table in postgresql

2. Run the following command to make a connection to your PostgreSQL database.

psql -U user_name -d database_name

Change user_name to the username you use with PostgreSQL, and database_name to the name of the database that you are using. For example, check the below picture.

how to create a new table in postgresql

3. You can create a table by running a SQL query once you’re connected to the database. The syntax to create a table in PostgreSQL is given below.

CREATE TABLE table_name(
column1 datatype,
column2 datatype,
...
);

Replace the table_name with the name you want for your table, then include the parenthesis to describe the columns and their appropriate data types. Columns can be added in any number, separated by commas.

For example, create a table called “country” with two columns “id” and “name”. Run the below command.

CREATE TABLE country(
id SERIAL PRIMARY KEY,
name VARCHAR(100)
);
how to create a temp table in postgresql

The above query creates a table with two columns, “id” as a serial primary key, and “name” as a variable character with a maximum length of 100 characters.

Let’s describe the created table using the below command.

\d country
How to Create a Table In PostgreSQL Terminal Describe Table

We have created the table named “country” using the terminal or command prompt in PostgreSQL.

Approach-2 Using pgAdmin

To create a new table in PostgreSQL, Follow the below steps.

1. Connect to your PostgreSQL server using pgAdmin by opening it. If it asks for a password, then enter the Postgres user password.

how to create a table in postgresql using pgadmin

2. Then, choose the server from the left sidebar. After choosing the server, if it asks for a password then provide the password.

create a table in postgresql using pgadmin

3. Navigate to the target database where you want to create the table by expanding the database tree in the left sidebar.

create a new table in postgresql

4. Select “Create” > “Table” from the menu by right-clicking the “Tables” folder.

how to create index on a table in postgresql

In the “General” tab, specify the table name like “country”.

how to create a temporary table in postgresql

5. Define the columns for your table on the “Columns” table of the table creation window. Add three columns, for example, with the following data types “id” as a “serial” and “name” as a “character varying” with a length of 100.

how to create a table in postgresql using python

6. To create a table click on “Save” as shown in the above picture. After this, you see the new table name “country” in the “Tables” folder in pgAdmin.

How to Create a Table in PostgreSQL pgAdmin

Conclusion

In this PostgreSQL tutorial, we have covered How to Create a Table In PostgreSQL using the terminal and pgAdmin.

You may like to read:

Top 200 SQL Server Interview Questions and Answers

Free PDF On Top 200 SQL Server Interview Questions And Answers

Download A 40 pages PDF And Learn Now.