execute-sql
The execute-sql
command executes a SQL string or file.
Uses
Use the execute-sql
command to directly run SQL queries without changing and applying changelog files with changesets. An example of using the execute-sql
command is to check the database data or make unplanned changes when you run Liquibase in automation.
Compared with other database query tools, the execute-sql
command runs with the credentials and configuration you already have in Liquibase without any need to set up another tool. If statements run correctly in your database CLI but fail in Liquibase, running exploratory statements through execute-sql
might help you find differences in permission configurations, connection setup, and SQL run in the database CLI or database-native SQL.
Note: The command does not affect the DATABASECHANGELOG table because the table is associated with changesets and tracks which changesets have been run. Liquibase recommends handling your changes with execute-sql
only on rare occasions to not lose their traceability.
Syntax
To run the execute-sql
command, specify the following parameters in the Liquibase properties file, environment variables, or the command prompt while running the command:
- URL, driver [optional], and user authentication information such as username and password.
sql
orsql-file
parameter.
Tip: For more information about the options to store parameters, see Create and Configure a liquibase.properties File, Liquibase Environment Variables, and JAVA_OPTS Environment Variable. To find the format of the parameter, see Liquibase Parameters.
Run the execute-sql
command with the SQL string:
liquibase execute-sql --sql="SELECT * FROM databasechangelog"
Run the execute-sql
command with the SQL file:
liquibase execute-sql --sql-file=path/to/file.sql
Note: You can also specify --delimiter=/
. The default value for delimiter is ;
, which will work as long as you do not have stored logic.
Parameters
Global parameters
Attribute | Definition | Requirement |
---|---|---|
|
File path to where the command output will be written: a CSV file that is the result of running a |
Optional |
Command parameters
Attribute | Definition | Requirement |
---|---|---|
|
The SQL string to execute Note: Columns display in the same order they are specified in the argument. If no columns are specified (For example |
Required |
|
Specifies the file where SQL statements are stored |
Required |
|
The JDBC database connection URL. See Using JDBC URL in Liquibase. |
Required |
|
Name of the default catalog to use for the database connection |
Optional |
|
Name of the default schema to use for the database connection. If Tip: In Liquibase v4.23.0+, camelCase for Note: The syntax |
Optional |
|
The delimiter to use when executing SQL scripts |
Optional |
|
The JDBC driver class |
Optional |
|
The JDBC driver properties file |
Optional |
|
Password to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
|
Username to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
Global parameters
Attribute | Definition | Requirement |
---|---|---|
|
File path to where the command output will be written: a CSV file that is the result of running a |
Optional |
Command parameters
Attribute | Definition | Requirement |
---|---|---|
|
The SQL string to execute Note: Columns display in the same order they are specified in the argument. If no columns are specified (For example |
Required |
|
Specifies the file where SQL statements are stored |
Required |
|
The JDBC database connection URL. See Using JDBC URL in Liquibase. |
Required |
|
Name of the default catalog to use for the database connection |
Optional |
|
Name of the default schema to use for the database connection. If Tip: In Liquibase v4.23.0+, camelCase for Note: The syntax |
Optional |
|
The delimiter to use when executing SQL scripts |
Optional |
|
The JDBC driver class |
Optional |
|
The JDBC driver properties file |
Optional |
|
Password to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
|
Username to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
Global parameters
Attribute | Definition | Requirement |
---|---|---|
|
File path to where the command output will be written: a CSV file that is the result of running a |
Optional |
Command parameters
Attribute | Definition | Requirement |
---|---|---|
|
The SQL string to execute Note: Columns display in the same order they are specified in the argument. If no columns are specified (For example |
Required |
|
Specifies the file where SQL statements are stored |
Required |
|
The JDBC database connection URL. See Using JDBC URL in Liquibase. |
Required |
|
Name of the default catalog to use for the database connection |
Optional |
|
Name of the default schema to use for the database connection. If Tip: In Liquibase v4.23.0+, camelCase for Note: The syntax |
Optional |
|
The delimiter to use when executing SQL scripts |
Optional |
|
The JDBC driver class |
Optional |
|
The JDBC driver properties file |
Optional |
|
Password to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
|
Username to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
Global parameters
Attribute | Definition | Requirement |
---|---|---|
|
File path to where the command output will be written: a CSV file that is the result of running a |
Optional |
Command parameters
Attribute | Definition | Requirement |
---|---|---|
|
The SQL string to execute Note: Columns display in the same order they are specified in the argument. If no columns are specified (For example |
Required |
|
Specifies the file where SQL statements are stored |
Required |
|
The JDBC database connection URL. See Using JDBC URL in Liquibase. |
Required |
|
Name of the default catalog to use for the database connection |
Optional |
|
Name of the default schema to use for the database connection. If Tip: In Liquibase v4.23.0+, camelCase for Note: The syntax |
Optional |
|
The delimiter to use when executing SQL scripts |
Optional |
|
The JDBC driver class |
Optional |
|
The JDBC driver properties file |
Optional |
|
Password to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
|
Username to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
Global parameters
Attribute | Definition | Requirement |
---|---|---|
|
File path to where the command output will be written: a CSV file that is the result of running a |
Optional |
Command parameters
Attribute | Definition | Requirement |
---|---|---|
|
The SQL string to execute Note: Columns display in the same order they are specified in the argument. If no columns are specified (For example |
Required |
|
Specifies the file where SQL statements are stored |
Required |
|
The JDBC database connection URL. See Using JDBC URL in Liquibase. |
Required |
|
Name of the default catalog to use for the database connection |
Optional |
|
Name of the default schema to use for the database connection. If Tip: In Liquibase v4.23.0+, camelCase for Note: The syntax |
Optional |
|
The delimiter to use when executing SQL scripts |
Optional |
|
The JDBC driver class |
Optional |
|
The JDBC driver properties file |
Optional |
|
Password to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
|
Username to connect to the target database. Tip: It is a best practice to store sensitive data in a Secrets Management tool with Liquibase Pro. |
Optional |
Note: The username
and password
attributes are not required for connections and systems which use alternate means of authentication. Also, you can specify database credentials as part of the url
attribute.
Liquibase Version: 4.9.1
Output of select * from databasechangelog:
AUTHOR | COMMENTS | CONTEXTS | DATEEXECUTED | DEPLOYMENT_ID | DESCRIPTION | EXECTYPE | FILENAME | ID | LABELS | LIQUIBASE | MD5SUM | ORDEREXECUTED | TAG |
liquibase | | null | 2022-05-13 12:02:12.125792 | 2464932010 | empty | EXECUTED | liquibase-internal | 1652464932094 | null | 4.9.1 | 8:d41d8cd98f00b204e9800998ecf8427e | 1 | version1 |
other.dev | example comment | example-context | 2022-05-13 12:17:30.818119 | 2465850648 | sql | EXECUTED | example-changelog.sql | 3 | example-label | 4.9.1 | 8:98b5f38170a3749834d15d3361fc6d9b | 4 | version1 |
Liquibase command 'execute-sql' was executed successfully.