pgcli icon indicating copy to clipboard operation
pgcli copied to clipboard

passing sql via command line a la '-c' for psql?

Open jonassteinberg1 opened this issue 4 years ago • 3 comments

Description

I don't see a 'sql via the command ilne switch' option which is surprising. Just want to make sure I'm not missing anything. I've tried just appending sql to the end of a database connection and also using '-c', neither of which work.

Your environment

MacOS

  • [x] Please provide your OS and version information. 10.15.7
  • [x] Please provide your CLI version. 3.1.0
  • [ ] What is the output of pip freeze command.

jonassteinberg1 avatar Sep 13 '21 13:09 jonassteinberg1

pgcli is designed for interactive use. We're not specifically against implementing non-interactive use cases, they just have not been a priority. We welcome any PRs.

j-bennet avatar Sep 27 '21 22:09 j-bennet

@jonassteinberg1 I'd recommend changing to use all the PG env vars for host/user/password then psql postgres < $HOME/sql/show_full_processlist.sql I've used this style on lots of different db platforms like postgres, mysql and others. pgcli is way better at being a SQL IDE interactively. Just the syntax highlighting is worth it's weight in gold. Great work to the devs on this one. Love it.

prpht9 avatar Jun 13 '22 19:06 prpht9

You can kind of do it in a hacky way if you really want to:

# set your env vars then
PGPASSWORD=$SRC_DB_PASS pgcli \
    --less-chatty \
    -h $SRC_DB_HOST \
    -u $SRC_DB_USER \
    $SRC_DB_NAME <<STDIN
  SELECT * \
  FROM information_schema.tables \
  WHERE table_schema = '$SRC_DB_SCHEMA';
STDIN

But to @j-bennet 's point, psql is likely easier to install and use like so:

PGPASSWORD=$SRC_DB_PASS psql \
    --csv \
    -h $SRC_DB_HOST \
    -U $SRC_DB_USER \
    $SRC_DB_NAME <<STDIN
  SELECT * \
  FROM information_schema.tables
  WHERE table_schema = '$SRC_DB_SCHEMA';
STDIN

ubergarm avatar Feb 28 '23 16:02 ubergarm