How to create a folder to save output in Postgresql? - postgresql

If I'd like to save the output from file1.sql to a new file file2.sql, I would use this command in terminal/cmd:
psql -U postgres -f file1.sql -o file2.sql
What if, though, I want file2.sql to be in a different folder?
If I try this command:
psql -U postgres -f file1.sql -o New/file2.sql,
that won't automatically make a new folder and will give an error. The New folder needs to exist before I can do this.
I need to this over many output files and many new folders. One obvious alternative would be to pre-create the required folders using Python, but really, is there no way Postgresql can create folders for me?

Use mkdir -p.
It will try to create a directory New if it doesn't exist or does nothing if it already exists.The && ensures that
your psql command runs only if the mkdir command succeeds.
mkdir -p New && psql -U postgres -f file1.sql -o New/file2.sql
If you want to run os commands inside psql, simply use \! <command> option within file1.sql and then output via \o option.
\! mkdir -p New
\o New/file2.sql

Related

Error in run sql file in postgres | No such file or directory

Problem
when in tried run sql file in psql shell...
give "No such file or directory" error!
$ ls
config.sql config.yaml
$ sudo -i -u postgres psql
postgres=# \i config.sql
config.sql: No such file or directory
thanks for your reply!
Quick solution:
-i => goes to user's home directory!
as result ./config.sql address is incorrect!
just use
$ psql -U <user_name>
postgres=# \i config.sql
man sudo tells you:
-i, --login
Run the shell specified by the target user's password database entry as a login shell. This means that login-specific
resource files such as .profile, .bash_profile or .login will be read by the shell. If a command is specified, it is passed
to the shell for execution via the shell's -c option.
In particular, that will set your current working directory to the home directory of user postgres.
If you want to avoid that, don't use '-i'.

How to get dump only function creation & Stored Process script via command line in PostgreSQL

I am able to get the script for table creation via pg_dump command in postgress sql similar way I need to generate for functions, SP's & views to export into SQL file via pg_dump.
Can you please help on this?
For a Table:
pg_dump -U postgres -d postgres -t clientlocationregions > C:\clientlocationregions.sql
Thanks
Mahesh
This will do:
pg_dump -Fc -s -n <schemaname> -f temp.dump <database name>
pg_restore -l temp.dump | grep FUNCTION > functionalist
pg_restore -L functionalist temp.dump > yourfunctions.sql

execute sql files from a folder in postgres

I have a set of sql files placed in a folder in postgres docker image. Whenever a new account is created, I want a new database to be created and all these scripts should get executed on that new created database.
New database will be created manually. I need to create sh file to ask for db name, host, port, password etc and then execute all sql files on new created database.
I tried and I can execute one single file
psql -h localhost -d sampledb -U superuser -f sample.sql
I need to execute all files in the folder. How I can achieve this.
Here, you can make use of small bash script.
Copy all your sql files to a folder, for example test.
Then execute the below line of script.
for i in $(ls -l test/*.sql |awk '{print $NF}'); do
psql -h localhost -d sampledb -U superuser -f $i;
done

Mongodump not stored in the specific location

I am doing a mongodump, and want to store it in a specific location. So, from reading online, I can do this using the following command:
sudo mongodump -d mydbs -u user -p password -o /myfolder/mongoBackups
I am using an EC2 instance, and when I run this command from my centos home folder, this is the output I get from mongodump:
2017-01-07T16:01:42.053+0000 writing mydbs.users to
2017-01-07T16:01:42.055+0000 done dumping mydbs.users (2 documents)
However, when I cannot find the folder myfolder/mongoBackups anywhere in the specific home/centos location where I run it. Any idea why?
It's not within home/centos
It's in /myfolder/mongoBackups
Specifying an output directory of /myfolder/mongoBackups is basically putting it in a folder called myfolder on the root of the hard drive.
If you truly want the folder to be within home/centos try
sudo mongodump -d mydbs -u user -p password -o /home/centos/myfolder/mongoBackups

How to execute multiple sql files in postgreSQL linux?

I have many .sql files in a folder (/home/myHSH/scripts) in linux debian. I want to know the command to execute or run all sql files inside the folder into postgreSQL v9.1 database.
PostgreSQL informations:
Database name=coolDB
User name=coolUser
Nice to have: if you know how to execute multiple sql files through GUI tools too like pgAdmin3.
From your command line, assuming you're using either Bash or ZSH (in short, anything but csh/tcsh):
for f in *.sql;
do
psql coolDB coolUser -f "$f"
done
The find command combined with -exec or xargs can make this really easy.
If you want to execute psql once per file, you can use the exec command like this
find . -iname "*.sql" -exec psql -U username -d databasename -q -f {} \;
-exec will execute the command once per result.
The psql command allows you to specify multiple files by calling each file with a new -f argument. e.g. you could build a command such as
psql -U username -d databasename -q -f file1 -f file2
This can be accomplished by piping the result of the find to an xargs command once to format the files with the -f argument and then again to execute the command itself.
find . -iname "*.sql" | xargs printf -- ' -f %s' | xargs -t psql -U username -d databasename -q