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.
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.
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) );
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.
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.
Then choose the server from the left sidebar. After choosing the server, if it asks for a password then provide the password.
Navigate to the target database where you want to create the table by expanding the database tree in the left sidebar.
Select “Create” > “Table” from the menu by right-clicking the “Tables” folder.
In the “General” tab, specify the table name like “country”.
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.
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.
In this PostgreSQL tutorial, we have covered How to Create a Table In PostgreSQL using the terminal and pgAdmin.
You may like to read:
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.