How to connect and execute a SQL file non interactively using psql? - windows

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.
What am I missing here? Thanks in advance.

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.

Related

Cannot get psql command to run in bash script

I'm a bit of a novice at bash scripting, so bear with me. I'm trying to write a script to execute a sql file using psql. From my terminal, it works fine:
psql -f /path/to/file.sql "$URI"
However, in my script I have something like this:
dbURI="postgres://some.connection.string"
psql -f /path/to/file.sql $dbURI
But I keep getting output like this:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
I cannot seem to get this to work at all. I've tried wrapping the variable in quotes, using $(command), etc, with no luck.
Try using the below in your script to disable globbing
psql -f /path/to/file.sql "$dbURI"
I have just had the same problem, with the exact same error message. The problem was that Postgresql takes a few seconds to start. So, if it is the case that you start postgresql and use a psql command in the same script, chances are that postgre has not yet started when you call it.
The solution was to include:
sleep 5
before the psql command. In your case, this would be:
sleep 5
psql -f /path/to/file.sql "$URI"
This gives some time for postgre to start before you use it.
I see the topic is 2 years old, but in case anyone else faces the same problem.

Unable to run psql command from within a BASH script

I have run into a problem with the psql command in my BASH script as I am trying to login to my local postgres database and submit a query. I am using the command in the following way:
psql -U postgres -d rebasoft_appauditor -c "SELECT * FROM katan_scripts"
However, I get the following error message.
psql: FATAL: Ident authentication failed for user "postgres"
This runs perfectly fine from the command line after I appended the following changes to /var/lib/pgsql/data/pg_hba.conf:
local all all trust
host all all 127.0.0.1/32 trust
Also, could this please be verified for correctness?
I find it rather strange that database authentication works fine on the command line but in a script it fails. Could anyone please help with this?
Note: I am using MAC OSX
It might possibly depend on your bash script.
Watch for the asterisk (*) not be replaced with the file names in your current directory. And possibly a semicolon or \g might help to actually send the SQL statement to the database server.

Repeated prompt for option while running SCP script

When I run an SCP script
scp -W Scp\password.txt -P 22 server:/file_location
scp -W Scp\password.txt -P 22 server:/file_location
in command prompt I get the following prompt:
Do you want to trust this new host key and continue connecting?
Please type 'no','once', or 'always':
As I attempt to type first letter of any option above, it keeps on repeating and prompts the same thing.
Please type 'no','once', or 'always':
Please type 'no','once', or 'always':
Please type 'no','once', or 'always':
How can I resolve this?
I had this same issue and found that it is resolved by copying and pasting the word you are trying to type in. For me I copied the work always and pasted it into cmd.

Weird windows prompt behavior restoring a dump with psql tool

I have just installed the postgresql 9.4.2 (64bits) on my windows 7 pro (64 bits) and created a database with the purpose to restore a dump which I generated like this:
pg_dump -O -U owner_user my_database > dump_my_database_20141122.sql
Now I am trying to restore this dump in this new created database with this command (like I used to do many times before):
psql –q –U owner_user my_database < dump_my_database_20141122.sql
But windows command prompt seems not to be understanding what the command should do. It is stating this:
psql: aviso: argumento extra de linha de comando "owner_user" ignorado
psql: aviso: argumento extra de linha de comando "my_database" ignorado
Senha para usuário -U:
That in english should be something like this:
psql: Warning: extra command line argument of "owner_user" ignored
psql: Warning: extra command line argument of "my_database" ignored
Password for -U user:
I've also tryied many variations of the command changing parameters order passing -f option in the "<" place and so on but nothing seems to work.
Had someone else had this problem? Am I doing something wrong? Is there some change in the psql command in postgresql 9.4.2 version that I'm not knowing about?
I've made a huge search on google but found nothing!
I was able to restore the dump doing this:
psql -U owner_user -f dump_my_database_20141122.sql my_database
It just worked using the "-f" option and removing the "-q" option. However "-q" option is still listed in the help. Maybe this options isn't valid anymore. Luckily the database is small, otherwise it would flood my console.

mysqldump command works when typed directly into command line - but not in a shell script

I am just getting started with shell scripts to save me typing in the same commands over and over. This command is used to copy a database over to a slave server as part of setting up MySQL database replication.
It works when typed into the command prompt directly:
mysqldump --host=192.168.1.1 –uUSER –pPASSWORD --opt database_name | mysql --host=192.168.1.2 –uUSER –pPASSWORD -C database_name
USER, PASSWORD and database_name all are replaced with their actual values in the real script.
When I type this command into a scripts.sh file, give it the execute permission, and then run it with ./scripts.sh I get:
'RROR1102 (42000): Incorrect database name 'database_name
mysqldump: Got errno 32 on write
What could be causing this error? Do I need to modify the command somehow when it is contained in a shell script?
The variable your database name is in has a CR at the end. You may need to run your script through dos2unix, or use one of the solutions on this site for stripping CRs from data if you're getting the database name from an external source.

Resources