> This page location: PostgreSQL JDBC > Creating Tables
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL JDBC: Creating Tables

**Summary**: in this tutorial, you will learn how to create tables in a PostgreSQL database from a Java program using JDBC.

## Creating table program

The following example shows how to create tables in the PostgreSQL database from a Java program:

```java
import java.sql.SQLException;

public class Main {
    public static void main(String[] args) {

        var sql = "CREATE TABLE products (" +
                "    id SERIAL PRIMARY KEY," +
                "    name VARCHAR(255) NOT NULL," +
                "    price DECIMAL(10, 2) NOT NULL" +
                ");";
        try (var conn =  DB.connect();
             var stmt = conn.createStatement()) {
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
    }
}
```

How it works.

First, construct a `CREATE TABLE` statement that creates the `products` table:

```java
var sql = "CREATE TABLE products (" +
            "    id SERIAL PRIMARY KEY," +
            "    name VARCHAR(255) NOT NULL," +
            "    price DECIMAL(10, 2) NOT NULL" +
            ");";
```

Second, [establish a connection](https://neon.com/postgresql/postgresql-jdbc/connecting-to-postgresql-database) to `sales` database on the local PostgreSQL server using the `DB` class:

```java
var conn =  DB.connect();
```

Third, create a `Statement` by calling the `createStatement()` method of the `Connection` object:

```java
var stmt = conn.createStatement()
```

Fourth, execute the `CREATE` `TABLE` statement by calling the `executeUpdate()` method.

```java
stmt.executeUpdate(sql);
```

The try-catch statement will display an error message if any `SQLException` occurs.

Since the `connect()` and `createStatement()` method calls are wrapped in a try-with-resources statement, the `Statement` and `Connection` will be closed properly.

If you run the program, it'll create the `products` in the `sales` database.

## Verify the table creation

First, open the Command Prompt on Windows or Terminal on Unix-like systems.

Second, connect to the `sales` database on the local PostgreSQL server using the `psql` client tool:

```bash
psql -U postgres -d sales
```

It'll prompt you for a password.

Third, use the `\dt` command to [show tables](../postgresql-administration/postgresql-show-tables) in the `sales` database:

```
\dt
```

Output:

```
          List of relations
 Schema |   Name   | Type  |  Owner
--------+----------+-------+----------
 public | products | table | postgres
(1 row)
```

The output indicates that the `products` table has been created successfully.

## Summary

- Call the `executeUpdate()` method of a Statement object to execute a `CREATE TABLE` statement to create a new table in the PostgreSQL database.

---

## Related docs (PostgreSQL JDBC)

- [Connecting to PostgreSQL](https://neon.com/postgresql/postgresql-jdbc/connecting-to-postgresql-database)
- [Inserting Data](https://neon.com/postgresql/postgresql-jdbc/insert)
- [Querying Data](https://neon.com/postgresql/postgresql-jdbc/query)
- [Updating Data](https://neon.com/postgresql/postgresql-jdbc/update)
- [Deleting Data](https://neon.com/postgresql/postgresql-jdbc/delete)
- [Calling PostgreSQL Stored Functions](https://neon.com/postgresql/postgresql-jdbc/call-postgresql-stored-function)
- [Managing Transactions](https://neon.com/postgresql/postgresql-jdbc/transaction)
