PostgreSQL supports several data types, including but not limited to:
1. Numeric Data Types:
- Integer (int): a whole number of varying sizes depending on the range needed (e.g., integer, smallint, bigint)
- Floating-point numbers: values with decimal points with varying ranges and precisions (e.g., float, decimal, numeric)
- Serial: a type of integer that automatically increments as values are added to a column
2. Character Data Types:
- Char (character): a fixed-length string of characters
- Varchar (variable character): a string of characters with varying length
3. Date and Time Data Types:
- Date: a date value (e.g., YYYY-MM-DD)
- Time: a time value (e.g., HH:MM:SS)
- Timestamp: a date and time value (e.g., YYYY-MM-DD HH:MM:SS)
- Interval: a period of time (e.g., 20 minutes)
4. Boolean Data Type:
- Boolean: true or false values
5. Array Data Type:
- Array: a collection of values of the same data type
6. Composite Data Types:
- Row: a collection of values of different data types
Examples of creating tables with different data types in Java:
import java.sql.*;
public class PostgresConnect {
public static void main(String[] args) {
final String url = "jdbc:postgresql://localhost/testdb";
final String user = "postgres";
final String password = "password";
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement statement = conn.createStatement()) {
String createTableQuery = "CREATE TABLE demo_table " +
"(id SERIAL PRIMARY KEY, " +
"name VARCHAR(255), " +
"age INT, " +
"dob DATE)";
statement.executeUpdate(createTableQuery);
} catch (SQLException e) {
System.out.println("Connection failure.");
e.printStackTrace();
}
}
}
This example creates a table called "demo_table" with four columns: "id" of type serial (an auto-incrementing integer), "name" of type varchar with a length of 255 characters, "age" of type integer, and "dob" of type date.