Share your experience with Database DevOps by taking part in the 2021 State of Database DevOps survey Take the survey

PostgreSQL

Supported Versions

Driver

URL format jdbc:postgresql://host:port/database
SSL support Yes - add ?ssl=true
Ships with Flyway Command-line Yes
Maven Central coordinates org.postgresql:postgresql:42.2.12[.jre6]
Supported versions 9.3-1104-jdbc4 and later
Default Java class org.postgresql.Driver

SQL Script Syntax

  • Standard SQL syntax with statement delimiter ;
  • Stored procedures (CREATE FUNCTION with $$ escapes, as generated by pg_dump)
  • COPY ... FROM STDIN (as generated by pg_dump)

Compatibility

  • DDL exported by pg_dump can be used unchanged in a Flyway migration.
  • Any PostgreSQL sql script executed by Flyway, can be executed by the PostgreSQL command-line tool and other PostgreSQL-compatible tools (after the placeholders have been replaced).
  • The driver shipped by default with the Flyway command line is postgresql-42.2.8.jre6.jar. This does not support SCRAM authentication. To use SCRAM, you will need to use Java 8 as a minimum and should download the postgresql-42.2.8.jar driver instead.

Authentication

PostgreSQL supports user and password being provided in the JDBC URL, in the form

jdbc:postgresql://<host>:<port>/<database>?user=myUsername&password=myPassword&<key1>=<value1>&<key2>=<value2>...

In this case, they do not need to be passed separately in configuration and the Flyway commandline will not prompt for them.

Example

/* Single line comment */
CREATE TABLE test_data (
 value VARCHAR(25) NOT NULL PRIMARY KEY
);


/*
Multi-line
comment
*/

-- Multi-statement PostgreSQL function
CREATE FUNCTION AddData() RETURNS INTEGER
AS $$
   BEGIN
    INSERT INTO test_data (value) VALUES ('Hello');
    RETURN 1;
  END;
$$ LANGUAGE plpgsql;

SELECT *  INTO TEMP adddata_temp_table FROM AddData() ;

-- Single-statement PostgreSQL function
CREATE FUNCTION add(integer, integer) RETURNS integer
   LANGUAGE sql IMMUTABLE STRICT
   AS $_$select $1 + $2;$_$;

-- Placeholder
INSERT INTO ${tableName} (name) VALUES ('Mr. T');

-- COPY ... FROM STDIN
COPY copy_test (c1, c2, c3) FROM stdin;
1	utf8: ümlaute: äüß	NaN
2	\N	123
3	text	123.234444444444449
\.

Limitations

  • No support for psql meta-commands with no JDBC equivalent like \set
  • Clean does not remove objects created by extensions. It is therefore highly recommended to create your extensions using CREATE EXTENSION IF NOT EXISTS in order to be able to clean and (re-)migrate your schema(s) at will.
  • No support for cleaning referenced large objects in pg_largeobject (Issue 1934)

Aurora PostgreSQL