1

I'm trying to pass a sql file to psql. After reading the docs, tried:

psql_args=(
    "password='$INPUT_PASSWORD'"
    dbname=analytics
    "host='$INPUT_HOST'"
    user=analytics
    port=32648
    file=query.sql
)

psql "${psql_args[*]}"
psql: error: invalid connection option "file"
root@380773cb4e26:/# 

If I remove the file=query.sql arg this results in a connection to psql. I just don't know how to pass it a query file?

On the docs, two arguments look like ones of interest here:

-f filename --file=filename

Read commands from the file filename, rather than standard input

and also:

-c command --command=command

Specifies that psql is to execute the given command string, command

I tried the file=query.sql one but that failed with the error message above.The command one wants a string whereas I want to pass a .sql file. I tried anyway:

psql_args=(
    "password='$INPUT_PASSWORD'"
    dbname=analytics
    "host='$INPUT_HOST'"
    user=analytics
    port=32648
    command=query.sql
)

psql "${psql_args[*]}"
psql: error: invalid connection option "command"

Is there a way that I can pass query.sql to psql in order to run a query?

4
  • Which language is this supposed to be? Commented Oct 23, 2021 at 11:43
  • psql - querying postgres from the terminal Commented Oct 23, 2021 at 11:50
  • But you are trying to call psql from the (bash) shell? Commented Oct 23, 2021 at 11:51
  • 1
    "${psql_args[*]}" expands your array as a single string. You need to expand your array into individual arguments with "${psql_args[@]}" instead. Commented Oct 23, 2021 at 12:57

2 Answers 2

3

You seem to be packaging options up into a connection string. But --file must be given directly as an option to psql, not as part of a connection string.

psql "${psql_args[*]}" --file=query.sql
Sign up to request clarification or add additional context in comments.

5 Comments

Can you fix your answer replacing "${psql_args[*]}" by "${psql_args[@]}" and including --file=query.sql withn the psql_args array?
@LéaGris I don't know these shell features very well, but that doesn't work for me. Then the other options start being misinterpreted.
read about bash array expansion @jjanes
I doubt just reading about it will cause the other options to start being interpreted correctly (particularly not 'password', as you can't just add two hyphens to it and have it work). Meanwhile, i already know how to solve the problem, by being less clever rather than more clever.
1

Since other answer seem to overlook this.

Here is how to store dynamic options into an array, and pass it as arguments to the command:

#!/usr/bin/env bash

psql_args=(
    "--dbname=analytics"
    "--host=$INPUT_HOST"
    "--user=analytics"
    "--port=32648"
    "--file=query.sql"
)

psql "${psql_args[@]}"

3 Comments

Thanks! Good to know.
But that doesn't work, as --password intentionally doesn't accept an argument in psql: psql: option '--password' doesn't allow an argument
@jjanes then remove the --password and this will be a good thing since passing a password as argument is a huge security risk anyway. Use the pgsql recommended authentication methods instead. How is this even an issue?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.