How to create a table in PostgreSQL [Terminal + pgAdmin]

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 Terminal

Open the command prompt or terminal.

How to Create a Table In PostgreSQL Terminal Opening Command Prompt

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 Table In PostgreSQL Terminal Connecting to Database
How to Create a Table In PostgreSQL Terminal Connecting to Database

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 Table In PostgreSQL Terminal

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.

How to Create a Table In PostgreSQL pgAdmin

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 pgAdmin Opening pgAdmin
How to Create a Table in PostgreSQL pgAdmin Opening pgAdmin

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

How to Create a Table in PostgreSQL pgAdmin Choosing Postgres Server
How to Create a Table in PostgreSQL pgAdmin Choosing Postgres Server

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

How to Create a Table in PostgreSQL pgAdmin Selecting Database
How to Create a Table in PostgreSQL pgAdmin Selecting Database

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

How to Create a Table in PostgreSQL pgAdmin Create Table
How to Create a Table in PostgreSQL pgAdmin Create Table

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

How to Create a Table in PostgreSQL pgAdmin Specify Table Name
How to Create a Table in PostgreSQL pgAdmin Specify Table Name

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 pgAdmin Column Creation
How to Create a Table in PostgreSQL pgAdmin Column Creation

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
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: