pg_dump: too many command-line arguments when calling from cmd - cmd

Im trying to make a backup in a folder C:\Users\Marko Petričević\Documents\Radni_sati_Backup\proba where "proba" is the name of backup file.
My command looks like this:
pg_dump -h 192.168.130.240 -p 5433 -U postgres -F c postgres > C:\Users\Marko Petričević\Documents\Radni_sati_Backup\proba
and then i get an error: " pg_dump: too many command-line arguments (first is "Petričević\Documents\Radni_sati_Backup\proba") "
But, when I write a command like:
pg_dump -h 192.168.130.240 -p 5433 -U postgres -F c postgres >C:\radni_sati_backup\radni_sati_proba
Everything works, and I get that "radni_sati_proba" file in the directory as I listed in command.
Why is this happening?

Found out what the problem was:
pg_dump -h 192.168.130.240 -p 5433 -U postgres -F c postgres > C:\Users\Marko Petričević\Documents\Radni_sati_Backup\proba
needs to be like this:
pg_dump -h 192.168.130.240 -p 5433 -U postgres -F c postgres > "C:\Users\Marko Petričević\Documents\Radni_sati_Backup\proba"
Problem was the space in path.

Related

How do I execute a command that may be in one of two paths?

I have those two paths :
C:/"Program Files"/PostgreSQL/14/bin
C:/"Program Files"/PostgreSQL/14/lib
and I have this command :
pg_dump -h localhost -U postgres -p 5432 -Fc -d notImportant > C:/Users/LENOVO/Documents/GitHub/app-reports-be/src/utils/sample.backup
pg_dump is locatet either in lib or bin. I am making an enviroment variable in my app in my .env file (I don't wanna have system enviroment variable). How can I tell my enviroment variable PATH to search first in bin and then in lib.
Like this :
C:/"Program Files"/PostgreSQL/14/lib & C:/"Program Files"/PostgreSQL/14/bin pg_dump -h localhost -U postgres -p 5432 -Fc -d notImportant > C:/Users/LENOVO/Documents/GitHub/app-reports-be/src/utils/sample.backup

Bash script, remote psql command over ssh, pipe sql file, password fails

I have a bash script. I want to run a postgres command with ssh that pipes a local file. The problem is the psql command prompts for a password, and my sql file gets piped into that. How do I write a command that pipes after I type in the password?
ssh server "psql -W -h db_host -p 5432 -U db_user -d postgres" < staging.sql
I suggest to break it down into multiple steps:
# Transfer the sql file to the server
scp staging.sql server
# Excute the queries in that file with psql over ssh
# Notes:
# - ssh -t enforces terminal allocation. You may try it without this option and see if it still works.
# - psql -f FILENAME reads commands from file
#
ssh -t server \
'psql -W -h db_host -U db_user -d postgres -f staging.sql; rm staging.sql'

How do I add '\c' to a psql command executed from a bash script?

To set up my project, I have to do quite a few commands and I'm trying to script this away. Some of these are in psql; so normally I'd go
psql -U postgres -h localhost -p 5433
(psql) create database test_database
(psql) \c test_database
(psql) \i integration-test/src/test/resources/init.sql
The .init.sql contains stuff to fill the database with mock data.
In my bash script, I tried reducing this to
psql -U postgres -h localhost -p 5433 -c "create database test_database; \c test_database; \i integration-test/src/test/resources/init.sql"
However, this gets me
ERROR: syntax error at or near "\"
LINE 1: create database test_database; \c fcs_analytics; \i integrat...
^
How do I execute these commands properly from my script?
Have you tried ?
psql -U postgres -h localhost -p 5433 << 'EOF'
create database test_database
\c test_database
\i integration-test/src/test/resources/init.sql
EOF

pg_dump command not found in bash programming

I create function in ~/.bash_profile file to backup postgresql database like this:
function dbbackup(){
pg_dump -h localhost -p 5432 -U myuser -W mydatabase > /my/path
}
I get an output:
dbbackup:1: command not found: pg_dump
If I run command directly in terminal:
pg_dump -h localhost -p 5432 -U myuser -W mydatabase > /my/path
It works. How can I do that? I need to run pg_dump programmatically using bash programming.

How can I keep repeating several commands until they all succeed?

I have a sequence of psql commands in a .sh script:
#!/bin/bash
psql -U postgres -1 -f file1.sql
psql -U postgres -1 -f file2.sql
psql -U postgres -1 -f file3.sql
psql -U postgres -1 -f file4.sql
psql -U postgres -1 -f file5.sql
I want to keep repeating these psql commands in block until they all succeed. So if for example the command for file4.sql fails, we should restart with file1.sql.
Should I do such logic in the .sql file or in the shell script?
while ! (psql -U postgres -f file1.sql
&& psql -U postgres -f file2.sql
&& psql -U postgres -f file3.sql
&& psql -U postgres -f file4.sql
&& psql -U postgres -f file5.sql)
do
echo retrying...
done

Resources