I have 2 SQL queries which I execute to get the size of table and number of records in a table
[~] mysql -u <username> -h <hostname> -p <db_name> -e "SQL_Query 1" > out.txt
[~] mysql -u <username> -h <hostname> -p <db_name> -e "SQL_Query 2" > out1.txt
How can I wite shell script to execute these queries
This is a shell script, supported by bash / sh, and probably others:
#!/bin/sh
mysql -u <username> -h <hostname> -p > output.log <<EOF
SELECT query 1 ...;
SELECT query 2 ...;
EOF
Note: You'll need to address the password entry issue, which can be done in several ways.
You can also enter your SQL in a file (file.sql) and redirect input from that file:
mysql -u <username> -h <hostname> -p < file.sql > output.log
Related
This question already has answers here:
PostgreSQL: Create schema in specific database
(3 answers)
Closed 3 months ago.
How to create the schema 'testschema' in the database 'testdb' using bash script?
The database is running in a docker container.
I've created a bash script that has following lines:
DB_NAME='testdb'
schm='testschema'
PGPASSWORD=$PGPASS psql -X -h localhost -p $DB_PORT -U postgres -c "CREATE DATABASE $DB_NAME;"
PGPASSWORD=$PGPASS psql -X -h localhost -p $DB_PORT -U postgres -c "CREATE SCHEMA $schm;"
After executing this it will create a schema but in postgres db. That is not what I want.
If I add a db name then it produces an error:
PGPASSWORD=$PGPASS psql -X -h localhost -p $DB_PORT -U postgres -c "CREATE SCHEMA $DB_NAME.$schm;"
Add a -d $DB_NAME switch to your psql calls, after creating this database.
PGPASSWORD=$PGPASS psql -X -h localhost -p $DB_PORT -U postgres -c "CREATE DATABASE $DB_NAME;"
PGPASSWORD=$PGPASS psql -X -h localhost -p $DB_PORT -U postgres -d $DB_NAME -c "CREATE SCHEMA $schm;"
Without specifying the database psql defaults to postgres.
From man psql:
-d dbname
--dbname=dbname
Specifies the name of the database to connect to. This is equivalent to specifying dbname as the first non-option argument on the command line. The dbname can be a connection string. If so, connection string parameters will override any conflicting command line options.
I need to execute the following sql queries from bash/expect script
what is the preferred approach to run these queries from bash script
# psql ambari -U ambari
Password for user ambari:
psql (9.2.24)
Type "help" for help.
ambari=>
ambari=>
ambari=>
ambari=> select
ambari-> sum(case when ulo = 1 then 1 else 0 end) as ulo_1,
ambari-> sum(case when ulo = 2 then 1 else 0 end) as ulo_2,
.
.
.
for access PostgreSQL we do
psql ambari -U ambari
Password for user ambari:bigdata
and when we run this ( /tmp/file include the bach of the query )
psql -U ambari -f /tmp/file ambari
we get
psql: FATAL: no pg_hba.conf entry for host "[local]", user "ambari", database "ambari", SSL off
I'm using this
dbhost=localhost
dbport=5432
dbuser=user
dbpass=pass
dbname=test
export PGPASSWORD="$dbpass"
dbopts="-h $dbhost -p $dbport -U $dbuser -d $dbname"
Then run sql script from file
psql $dbopts < "$path_to_sql_script"
Or from query var
query="
SELECT 1;
...
"
psql $dbopts <<< "$query"
Also pgpass can be set in special file ~/.pgpass like this
echo "$dbhost:$dbport:$dbname:$dbname:$dbpass" > ~/.pgpass
chmod 600 ~/.pgpass
Use switches -c command or -f filename, ie.:
$ psql -U ambari -c "SELECT ... ;" ambari # > result.file
or:
$ cat file.sql
SELECT
... ;
$ psql -U ambari -f file.sql ambari # > result.file
Probably -f as your query seems lengthy. Use > result.file to store the query result to a file.
As for the password, store following kind of entry to .pgpass file in user's home dir:
$ cat >> ~/.pgpass
#hostname:port:database:username:password
localhost:5432:ambari:ambari:t00M4NY53CR3t5
and set its rights to user's eyes only:
$ chmod 600 ~/.pgpass
Also, consider psql -h hostname if the database is not running in localhost (this needs to reflect in .pgpass entry as well).
I want to connect to sybase than execute a file.sql
I tapped this command:
sql -U Login -P MotDePasse -S ServeurASE -d NomDeLaBase -i Script.sql -o JournalDErreur.log
I also tried
isql -U Login -P MotDePasse -S #IPserveur:Port -d NomDeLaBase -i Script.sql -o JournalDErreur.log
and I have the error :
"La commande sql n'est pas reconnue'
Can u help please?
I have installed JTDS and sql (pip install) but it doesn't work
Assuming you're trying to connect to a Sybase ASE database, and you're using the isql command line utility that comes with ASE (or the Sybase SDK), the correct isql format is:
isql -U <login> -P <password> -S <host_or_ipaddr>:<port> -D <dbname> -i <input_script> -o <output_file>
So updating your example we get:
isql -U Login -P MotDePasse -S IPserveur:Port -D NomDeLaBase -i Script.sql -o JournalDErreur.log
I can't speak for your sql example as I'm not familiar with that tool let alone its input parameters.
I am new in SQL Server, can somebody help me to execute SQL query from command-line tool in SQL Server?
use below command prompt query to execute filename.sql using SQLCMD.
SQLCMD -d <database-name> -U <User-name> -P <Password> -i filename.sql -o output.txt
-d = Database Name
-U = User Name
-P = Password
-i = Filename.sql
-o = output.txt
-E = Windows Authentication mode if you specify this need to skip -U & -P parameters.
I need to write the results of executing a hive query to a file. how do i do it? currently, it's printing to the console.
beeline -u db_url -n user_name -p password -f query.sql
i tried:
beeline -u db_url -n user_name -p password -f query.sql 2> output.txt
but output.txt just contains when connection started and closed, not the results of the query - which are still being printed to the console.
I assume beeline -u db_url -n user_name -p password -f query.sql > output.txt must be OK. Without 2
"2" in your command is errlog, not the stdout
so "...query.sql 2> output.txt" would put the errlog results into your text file, while "...query.sql > output.txt" would put the actual output into the text file.
In addition to #dds 's answer you can try adding the silent feature to get rid of all the other stuffs like the connection started and closed status being printed in the output file.
beeline -u db_url -n user_name -p password --silent=true -f query.sql > output.txt
I think you meant to type "csv2" instead of "csv 2". Here's the fixed command line:
beeline -u db_url -n user_name -p password -f query.sql2 > output.txt