queries interactively, issue them to
PostgreSQL , and see the query results.
Alternatively, input can be from a file or from command line
- arguments. In addition, it provides a number of meta-commands and various
- shell-like features to facilitate writing scripts and automating a wide
- variety of tasks.
+ arguments. In addition,
psql provides a
+ number of meta-commands and various shell-like features to
+ facilitate writing scripts and automating a wide variety of tasks.
--command=command >
- Specifies that
psql is to execute the given
- command string, command .
- This option can be repeated and combined in any order with
- the -f option.
+ Specifies that
psql is to execute the given
+ command string, command .
+ This option can be repeated and combined in any order with
+ the -f option. When either -c
+ or
-f is specified,
psql
+ does not read commands from standard input; instead it terminates
+ after processing all the -c and -f
+ options in sequence.
- command must be either
- a command string that is completely parsable by the server (i.e.,
- it contains no
psql -specific features),
- or a single backslash command. Thus you cannot mix
- meta-commands with this option. To achieve that, you could
- use repeated -c options or pipe the string
- into
psql , for example:
- psql -c '\x' -c 'SELECT * FROM foo;' or
- echo '\x \\ SELECT * FROM foo;' | psql .
- (\\> is the separator meta-command.)
+ command must be either
+ a command string that is completely parsable by the server (i.e.,
+ it contains no
psql -specific features),
+ or a single backslash command. Thus you cannot mix
+ meta-commands within a -c option. To achieve that,
+ you could use repeated -c options or pipe the string
+ into
psql , for example:
+psql -c '\x' -c 'SELECT * FROM foo;'
+
+ or
+echo '\x \\ SELECT * FROM foo;' | psql
+
+ (\\> is the separator meta-command.)
- Each command string passed to -c is sent to the server
- as a single query. Because of this, the server executes it as a single
- transaction, even if a command string contains
- multiple
SQL commands, unless there are
- explicit BEGIN>/COMMIT> commands included in the
- string to divide it into multiple transactions. Also, the server only
-
returns the result of the last SQL command to the
- client. This is different from the behavior when the same string with
- multiple
SQL commands is fed
- to
psql 's standard input because
- then
psql sends each
SQL
- command separately.
+ Each
SQL command string passed
+ to -c is sent to the server as a single query.
+ Because of this, the server executes it as a single transaction even
+ if the string contains multiple
SQL commands,
+ unless there are explicit BEGIN>/COMMIT>
+ commands included in the string to divide it into multiple
+
transactions. Also, psql only prints the
+ result of the last
SQL command in the string.
+ This is different from the behavior when the same string is read from
+ a file or fed to
psql 's standard input,
+ because then
psql sends
+
each SQL command separately.
- Putting more than one command in the -c string often
- has unexpected results. This is a result of the fact that the whole
- string is sent to the server as a single query.
+ Because of this behavior, putting more than one command in a
+ single -c string often has unexpected results.
It's better to use repeated -c commands or feed
multiple commands to
psql 's standard input,
either using
echo as illustrated above, or
--file=filename >
- Use the file filename
- as the source of commands instead of reading commands interactively.
- This option can be repeated and combined in any order with
- the -c option. After the commands in
- every -c command string and -f file
- are processed,
psql terminates. This option
- is in many ways equivalent to the meta-command \i .
+ Read commands from the
+ file filename ,
+ rather than standard input.
+ This option can be repeated and combined in any order with
+ the -c option. When either -c
+ or
-f is specified,
psql
+ does not read commands from standard input; instead it terminates
+ after processing all the -c and -f
+ options in sequence.
+ Except for that, this option is largely equivalent to the
+ meta-command \i .
If filename is -
- (hyphen), then standard input is read.
+ (hyphen), then standard input is read until an EOF indication
+ or \q> meta-command. This can be used to intersperse
+ interactive input with input from files. Note however that Readline
+ is not used in this case (much as if -n had been
+ specified).
--single-transaction
- When
psql executes commands from a script
- and/or a -c option, adding this option
- wraps BEGIN>/COMMIT> around all of those
- commands as a whole to execute them as a single transaction. This
- ensures that either all the commands complete successfully, or no
- changes are applied.
+ This option can only be used in combination with one or more
+ -c and/or -f options. It causes
+
psql to issue a
BEGIN> command
+ before the first such option and a COMMIT> command after
+ the last one, thereby wrapping all the commands into a single
+ transaction. This ensures that either all the commands complete
+ successfully, or no changes are applied.
Notes
-
- In an earlier life
psql allowed the
- first argument of a single-letter backslash command to start
- directly after the command, without intervening whitespace.
- As of
PostgreSQL 8.4 this is no
- longer allowed.
-
-
-
psql works best with servers of the same
or an older major version. Backslash commands are particularly likely
If you want to use
psql to connect to several
servers of different major versions, it is recommended that you use the
newest version of
psql . Alternatively, you
- can keep a
copy of psql from each major
- version around and be sure to use the version that matches the
+ can keep a
round a copy of psql from each
+ major version and be sure to use the version that matches the
respective server. But in practice, this additional complication should
not be necessary.
- Before
PostgreSQL 9.6,
-c
- implied -X ; this is no longer the case.
+ the -c option implied -X
+ (--no-psqlrc>); this is no longer the case.
+
+
+
+
+ first argument of a single-letter backslash command to start
+ directly after the command, without intervening whitespace.
+ Now, some whitespace is required.
typedef struct SimpleActionListCell
{
struct SimpleActionListCell *next;
- int action;
- char *val;
+ enum _actions action;
+ char *val;
} SimpleActionListCell;
typedef struct SimpleActionList
static void parse_psql_options(int argc, char *argv[],
struct adhoc_opts * options);
+static void simple_action_list_append(SimpleActionList *list,
+ enum _actions action, const char *val);
static void process_psqlrc(char *argv0);
static void process_psqlrc_file(char *filename);
static void showVersion(void);
-static void simple_action_list_append(SimpleActionList *list,
- int action, const char *val);
static void EstablishVariableSpace(void);
#define NOPAGER 0
SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
- options.actions.head = NULL;
- options.actions.tail = NULL;
-
parse_psql_options(argc, argv, &options);
/*
process_psqlrc(argv[0]);
/*
- * If any actions were given by caller, process them in the order in
- * which they were specified .
+ * If any actions were given by user, process them in the order in which
+ * they were specified. Note single_txn is only effective in this mode .
*/
if (options.actions.head != NULL)
{
- PGresult *res;
- SimpleActionListCell *cell;
+ PGresult *res;
+ SimpleActionListCell *cell;
successResult = EXIT_SUCCESS; /* silence compiler */
scan_state = psql_scan_create();
psql_scan_setup(scan_state,
- cell->val,
- strlen(cell->val));
+ cell->val,
+ strlen(cell->val));
successResult = HandleSlashCmds(scan_state, NULL) != PSQL_CMD_ERROR
? EXIT_SUCCESS : EXIT_FAILURE;
else
{
/* should never come here */
- Assert(0 );
+ Assert(false );
}
if (successResult != EXIT_SUCCESS && pset.on_error_stop)
if (optarg[0] == '\\')
simple_action_list_append(&options->actions,
ACT_SINGLE_SLASH,
- pstrdup(optarg + 1) );
+ optarg + 1 );
else
simple_action_list_append(&options->actions,
ACT_SINGLE_QUERY,
- pstrdup(optarg) );
+ optarg );
break;
case 'd':
options->dbname = pg_strdup(optarg);
break;
case 'f':
simple_action_list_append(&options->actions,
- ACT_FILE,
- pg_strdup(optarg) );
+ ACT_FILE,
+ optarg );
break;
case 'F':
pset.popt.topt.fieldSep.separator = pg_strdup(optarg);
}
+/*
+ * Append a new item to the end of the SimpleActionList.
+ * Note that "val" is copied if it's not NULL.
+ */
+static void
+simple_action_list_append(SimpleActionList *list,
+ enum _actions action, const char *val)
+{
+ SimpleActionListCell *cell;
+
+ cell = (SimpleActionListCell *) pg_malloc(sizeof(SimpleActionListCell));
+
+ cell->next = NULL;
+ cell->action = action;
+ if (val)
+ cell->val = pg_strdup(val);
+ else
+ cell->val = NULL;
+
+ if (list->tail)
+ list->tail->next = cell;
+ else
+ list->head = cell;
+ list->tail = cell;
+}
+
+
/*
* Load .psqlrc file, if found.
*/
}
-/*
- * Support for list of actions. SimpleStringList cannot be used due possible
- * combination different actions with the requirement to save the order.
- */
-static void
-simple_action_list_append(SimpleActionList *list, int action, const char *val)
-{
- SimpleActionListCell *cell;
- size_t vallen = 0;
-
- if (val)
- vallen = strlen(val);
-
- cell = (SimpleActionListCell *)
- pg_malloc(offsetof(SimpleActionListCell, val) + vallen + 1);
-
- cell->next = NULL;
- cell->action = action;
- if (val)
- {
- cell->val = pg_malloc(vallen + 1);
- strcpy(cell->val, val);
- }
- else
- cell->val = NULL;
-
- if (list->tail)
- list->tail->next = cell;
- else
- list->head = cell;
- list->tail = cell;
-}
-
static void
EstablishVariableSpace(void)
{