2

I am trying to run the following command using psql but the --file argument is ignored event tough I manage to connect to the database.

psql "postgresql://postgres:admin12345@localhost:5432/Phoenix" -f "tables/book.sql"

The output on the terminal is as the screenshot shows.

Command output

What am I missing here? Thanks in advance.

5
  • It might just be your font, but the -f doesn't look like a proper f Commented Dec 30, 2021 at 17:57
  • 1
    Heed the warning and look at "Notes for Windows Users" here psql. Commented Dec 30, 2021 at 18:35
  • @Bergi you're right, it's just the font lol. Commented Dec 30, 2021 at 21:08
  • What kind of terminal is that? Commented Dec 31, 2021 at 0:15
  • @Bergi it's a console emulator called Cmder. You can find it here cmder.net . Commented Dec 31, 2021 at 10:29

1 Answer 1

3

After googling and wasting hours on testing different SO answers, this worked for me:

psql -f "tables/book.sql" "postgresql://postgres:admin12345@localhost:5432/Phoenix"

It looks like the psql command requires that the command-line arguments appear before the connection string. That's a bit weird but it worked for me.

Also the quotes are optional. This also works:

psql -f tables/book.sql postgresql://postgres:admin12345@localhost:5432/Phoenix

I tried changing code page as indicated on the section "Notes for Windows Users" in the psql doc psql docmentation. The result is the warning does not show anymore but the command line args are still ignored if the connection string precedes them like as shown in the following logs:

C:\Users\Zack
λ cmd.exe /c chcp 1252

Active code page: 1252

C:\Users\Zack
λ psql postgresql://postgres:admin12345@localhost:5432/Phoenix -f "tables/book.sql"

psql: warning: extra command-line argument "tables/book.sql" ignored
psql (13.2)
Type "help" for help.

Phoenix=#

Theo only fix that works for me now is making the other args preced the connection string.

If someone knows more about why command-line args should precede the connection string, please provide more details.

Sign up to request clarification or add additional context in comments.

4 Comments

They don't have to precede the connection string. psql "postgresql://postgres:@localhost:5432/test" -f "test_sql.sql" and psql -d test -U postgres -f "test_sql.sql" work just fine for me on Linux.
@AdrianKlaver I'm not on Linux machine. It didn't not work for me on Windows.
I know. Read the section "Notes for Windows Users" in the link I posted in my earlier comment.
@AdrianKlaver I tried those instruction and I still get the same behavior. I updated my answer to give more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.