PostgreSQL database support module for Flyway migration tool with specialized implementations for PostgreSQL and CockroachDB databases
—
Core PostgreSQL database implementation providing database type registration, connection factory, and database-specific operations for the Flyway migration framework.
Registers PostgreSQL database support with Flyway and provides factory methods for creating database instances.
/**
* PostgreSQL database type implementation
*/
public class PostgreSQLDatabaseType extends BaseDatabaseType {
/**
* Returns the database name identifier
* @return "PostgreSQL"
*/
public String getName();
/**
* Returns list of supported database engines
* @return List containing "PostgreSQL", "AuroraPostgreSql", "YugabyteDb", "TimescaleDb"
*/
public List<String> getSupportedEngines();
/**
* Returns the null type identifier for this database
* @return Types.NULL
*/
public int getNullType();
/**
* Checks if this database type can handle the given JDBC URL
* @param url JDBC URL to check
* @return true if URL is PostgreSQL-compatible
*/
public boolean handlesJDBCUrl(String url);
/**
* Returns the appropriate JDBC driver class for the URL
* @param url JDBC URL
* @param classLoader Class loader for loading driver
* @return Driver class name (org.postgresql.Driver, com.p6spy.engine.spy.P6SpyDriver, or software.amazon.jdbc.Driver)
*/
public String getDriverClass(String url, ClassLoader classLoader);
/**
* Checks if this database type handles the given database product name and version
* @param databaseProductName Database product name from JDBC metadata
* @param databaseProductVersion Database version
* @param connection JDBC connection
* @return true if product name starts with "PostgreSQL"
*/
public boolean handlesDatabaseProductNameAndVersion(String databaseProductName,
String databaseProductVersion,
Connection connection);
/**
* Creates a PostgreSQL database instance
* @param configuration Flyway configuration
* @param jdbcConnectionFactory Connection factory
* @param statementInterceptor Statement interceptor
* @return PostgreSQLDatabase instance
*/
public Database createDatabase(Configuration configuration,
JdbcConnectionFactory jdbcConnectionFactory,
StatementInterceptor statementInterceptor);
/**
* Creates a PostgreSQL parser instance
* @param configuration Flyway configuration
* @param resourceProvider Resource provider for loading SQL files
* @param parsingContext Parsing context
* @return PostgreSQLParser instance
*/
public Parser createParser(Configuration configuration,
ResourceProvider resourceProvider,
ParsingContext parsingContext);
/**
* Sets default connection properties for PostgreSQL
* @param url JDBC URL
* @param props Properties to modify
* @param classLoader Class loader
*/
public void setDefaultConnectionProps(String url, Properties props, ClassLoader classLoader);
/**
* Gets external authentication properties (e.g., pgpass file support)
* @param url JDBC URL
* @param username Username
* @return Authentication properties
*/
public Properties getExternalAuthProperties(String url, String username);
}Usage Examples:
import org.flywaydb.database.postgresql.PostgreSQLDatabaseType;
import org.flywaydb.core.api.configuration.Configuration;
// The database type is automatically registered via SPI
// Check if a URL is handled by PostgreSQL type
PostgreSQLDatabaseType dbType = new PostgreSQLDatabaseType();
boolean canHandle = dbType.handlesJDBCUrl("jdbc:postgresql://localhost:5432/mydb");
// Get the appropriate driver class
String driverClass = dbType.getDriverClass("jdbc:postgresql://localhost:5432/mydb",
getClass().getClassLoader());Main database implementation providing PostgreSQL-specific operations and migration table management.
/**
* PostgreSQL database implementation
*/
public class PostgreSQLDatabase extends Database<PostgreSQLConnection> {
/**
* Creates a new PostgreSQL database instance
* @param configuration Flyway configuration
* @param jdbcConnectionFactory Connection factory
* @param statementInterceptor Statement interceptor
*/
public PostgreSQLDatabase(Configuration configuration,
JdbcConnectionFactory jdbcConnectionFactory,
StatementInterceptor statementInterceptor);
/**
* Creates a PostgreSQL connection wrapper
* @param connection Raw JDBC connection
* @return PostgreSQLConnection instance
*/
protected PostgreSQLConnection doGetConnection(Connection connection);
/**
* Ensures the PostgreSQL version is supported by Flyway
* @param configuration Flyway configuration
* @throws FlywayException if version is not supported
*/
public void ensureSupported(Configuration configuration);
/**
* Generates the SQL script for creating the Flyway schema history table
* @param table The table to create
* @param baseline Whether to include baseline entry
* @return SQL script for table creation
*/
public String getRawCreateScript(Table table, boolean baseline);
/**
* Gets the current database user
* @return Current user name
* @throws SQLException if user cannot be determined
*/
protected String doGetCurrentUser() throws SQLException;
/**
* Indicates whether PostgreSQL supports DDL transactions
* @return true (PostgreSQL supports DDL in transactions)
*/
public boolean supportsDdlTransactions();
/**
* Returns the PostgreSQL boolean true literal
* @return "TRUE"
*/
public String getBooleanTrue();
/**
* Returns the PostgreSQL boolean false literal
* @return "FALSE"
*/
public String getBooleanFalse();
/**
* Quotes a database identifier for PostgreSQL
* @param identifier The identifier to quote
* @return Quoted identifier with double quotes
*/
public String doQuote(String identifier);
/**
* Returns the escaped quote character for PostgreSQL
* @return "\"\""
*/
public String getEscapedQuote();
/**
* Indicates whether catalog equals schema in PostgreSQL
* @return false
*/
public boolean catalogIsSchema();
/**
* Determines whether to use a single connection based on configuration
* @return true if transactional lock is disabled, false otherwise
*/
public boolean useSingleConnection();
/**
* Gets the SELECT statement for querying the schema history table
* Includes special comment for pgpool load balancing
* @param table The schema history table
* @return SELECT statement with load balancing hint
*/
public String getSelectStatement(Table table);
/**
* Detects the database hosting environment
* @return "AWS RDS" if running on AWS RDS, otherwise default hosting
*/
public String getDatabaseHosting();
}Usage Examples:
import org.flywaydb.database.postgresql.PostgreSQLDatabase;
import org.flywaydb.core.api.configuration.ClassicConfiguration;
// Typically created via PostgreSQLDatabaseType.createDatabase()
Configuration config = new ClassicConfiguration();
PostgreSQLDatabase database = new PostgreSQLDatabase(config, connectionFactory, interceptor);
// Check DDL transaction support
boolean supportsDdl = database.supportsDdlTransactions(); // returns true
// Quote an identifier
String quoted = database.doQuote("my_table"); // returns "\"my_table\""
// Generate schema history table creation script
Table historyTable = database.getTable("flyway_schema_history");
String createScript = database.getRawCreateScript(historyTable, false);jdbc:postgresql://host:port/databasejdbc:p6spy:postgresql://host:port/database (P6Spy proxy)The package automatically detects AWS RDS environments and provides:
Install with Tessl CLI
npx tessl i tessl/maven-org-flywaydb--flyway-database-postgresql