PostgreSQL

PostgreSQL Create Table

Create a PostgreSQL table with SQL, define columns, and verify the result in pgAdmin 4.

Before you begin: Connect to a PostgreSQL database in pgAdmin 4 and open the Query Tool. If you are not connected yet, follow the pgAdmin 4 guide first.

Connect to the database

Once you are connected to the database, you can write SQL statements in the Query Tool. The example below creates a table named products.

Create a table

Run the following SQL statement:

CREATE TABLE products (
  name VARCHAR(255),
  model VARCHAR(255),
  year INT
);
CREATE TABLE products SQL statement in pgAdmin 4
Write the CREATE TABLE statement in the pgAdmin 4 Query Tool.

The statement creates an empty products table with three fields: name, model, and year. Execute the statement with the run button or by pressing F5.

Successful CREATE TABLE message in pgAdmin 4
pgAdmin 4 confirms that the CREATE TABLE statement completed successfully.

SQL statement explained

Every column needs a data type. The VARCHAR(255) type stores text with up to 255 characters, which is suitable for the product name and model. The INT type stores whole numbers, which fits the product year.

In production tables, also consider adding a primary key, NOT NULL constraints, and suitable indexes. For example, an application table often uses an identity or sequence-backed numeric identifier.

Products table visible in the pgAdmin 4 browser
Refresh the object browser to see the new products table.

Display the table

Use a SELECT statement to read the table:

SELECT * FROM products;
SELECT query and products table output in pgAdmin 4
Query the products table and view its columns in the Data Output panel.
Next step: Add rows with INSERT, change rows with UPDATE, and remove rows with DELETE. Keep schema changes in version-controlled migration scripts for application projects.

Further Reading

Continue learning with these related tutorials:

Continue learning