How to disable bash info prints - sql

Im running the next postgres query using the next bash command.
sudo -u postgres bash -c "psql -d db -c \"SELECT ip FROM db_accounts;\"" \>/dev/null
The output is a table but before the table is printed, I get the following info prints
> psql: /usr/lib64/libssl.so.10: no version information available
> (required by psql) psql: /usr/lib64/libcrypto.so.10: no version
> information available (required by /usr/pgsql-9.4/lib/libpq.so.5)
> psql: /usr/lib64/libssl.so.10: no version information available
> (required by /usr/pgsql-9.4/lib/libpq.so.5)
I want to run my command without these prints appearing.
I tried to change the end of the command >/dev/null to 2>/dev/null and indeed the prints were disable but my table was not fully displayed (out of 800 rows only 40 were displayed),
Can someone help me please?

Use --quiet when you start psql
OR
It can be set in your postgresql.conf file by adding this
client_min_messages = warning
This blog is really helpful.

To fix want I wanted I added --pset pager=off to the psql to get the whole table and the disable the prints I change the end of the command to 2>/dev/null
final command:
sudo -u postgres bash -c "psql --pset pager=off --quiet -d db -c \"SELECT ip FROM db_accounts;\"" 2>/dev/null

Related

mysql command displays `-?` help instead of `-e` executing command when run from bash script (but not when run by hand)

I have a rather simple bash script.
#!/bin/bash
echo
echo What is the root statement for the new debate?
read body
echo
echo What is your mysql password?
read -s pass
echo
sql='INSERT
INTO `Debate` (`unblocked`, `debaterId`, `dirty` )
VALUES ('"'1'"', '"'15'"', '"'0'"' );
INSERT
INTO `Statement` (`body`, `debateId`, `debaterId` )
VALUES ('"'"${body}"'"', LAST_INSERT_ID(), '"'15'"' );'
#echo mysql -u resolution -p ${pass} -D resolution -e \"${sql//[^a-zA-Z0-9(),;\`\'_]/ }\"
mysql -u resolution -p ${pass} -D resolution -e \"${sql//[^a-zA-Z0-9(),;\`\'_]/ }\"
What this is meant to do should be pretty straight forward but it comes down to "ask user for input on terminal... form sql command with the provided input... insert into database in two tables." It's meant to be a quick hack for adding new debates into the database for testing while development is ongoing.
Instead, mysql prints the help as if I had used -?. No error... just the help text. Everything looks correct when the mysql command is echoed to the terminal. And if I copy and paste the echoed command it works just fine. I've searched google and stack overflow but found nothing about this.
U#H ~ mysql --version
mysql Ver 15.1 Distrib 10.3.28-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

running postgresql image with podman failed

When running postgresql alpine image with podman :
podman run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=test -e POSTGRES_USER=test -d postgres:11-alpine
the result is :
Error: /usr/bin/slirp4netns failed: "open(\"/dev/net/tun\"): No such device\nWARNING: Support for sandboxing is experimental\nchild failed(1)\nWARNING: Support for sandboxing is experimental\n"
The running system is archlinux. Is there a way to fix this error or a turn arround ?
Thanks
Is slirp4netns correctly installed? Check the project Site for information.
Sometimes the flag order matters. try -d first and -p last (directly infornt of the image) looking like:
podman run -d --name postgres -e POSTGRES_PASSWORD=test -e POSTGRES_USER=test -p 5432:5432 postgres:11-alpine
Try only creating the neccessary password, then log into your container and create manually (this always worked for me)
podman run -d --name postgres -e POSTGRES_PASSWORD=test -p 5432:5432 postgres:11-alpline
podman exec -it postgres bash
Create default user postgres
su - postgres
start postgres
psql
create databases and tables
CREATE USER testuser WITH PASSWORD 'testpassword' | Doku
CREATE DATABASE testdata WITH OWNER testuser
Check if it worked
\l+
Connect to your Database via IP and Port
I assume you upgraded Arch packages recently. Most likely your system needs a restart.

Sudo over SSH mixes up password tty and stdin

Setup:
Local *nix machine with a SQL script script.sql (Postgres).
Remote machine remote (Debian 7) with Postgres.
I can SSH in as some_user, who is a sudoer.
Anything with Postgres needs to be done as postgres user.
The server only listens on localhost:5432.
How do I execute script.sql on remote without copying it there first?
This works well:
ssh -t some_user#remote 'sudo -u postgres psql -c "COMMANDS FOO BAR"'
The -t flag means that sudo will ask for some_user's password correctly on the local terminal.
One thing remains, to be able to pipe script.sql to psql. This does not work:
ssh -t some_user#remote 'sudo -u postgres psql' < script.sql
It fails with the message:
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: no tty present and no askpass program specified
Edit: simplified example
Postgres and psql don't seem to figure much in the problem. The following code has the same issues:
ssh some_user#remote xargs sudo ls < input_file
The problem seems to be: we need to send 2 inputs to sudo, both the password using a tty, and the stdin to pass to ls.
Edit: even simpler
ssh localhost xargs sudo ls < input_file
sudo: no tty present and no askpass program specified
Adding -t does not work:
$ ssh -t localhost xargs sudo ls < input_file
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: no tty present and no askpass program specified
Adding another -t does not work either:
$ ssh -t -t localhost xargs sudo ls < input_file
<content of input_file>
<waiting on a prompt>
ssh -T some_user#remote "sudo -u postgres psql -f-" < script.sql
"-f-" will read the script from STDIN. Just redirect the file in there, and there you go.
Don't bother with -t option to ssh, you don't need a full terminal for this.
ssh -T ${user}#${ip} sudo DEBIAN_FRONTEND=noninteractive postgres psql -f- < test.sql
Use DEBIAN_FRONTEND=noninteractive for resolve no tty present or equivalent of your distribution.

How to execute postgres' sql queries from batch file?

I need to execute SQL from batch file.
I am executing following to connect to Postgres and select data from table
C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
select * from test;
I am able to connect to database, however I'm getting the error
'select' is not recognized as an internal or external command,
operable program or batch file.
Has anyone faced such issue?
This is one of the query i am trying, something similar works in shell script, (please ignore syntax error in the query if there are any)
copy testdata (col1,col2,col3) from '%filepath%/%csv_file%' with csv;
You could pipe it into psql
(
echo select * from test;
) | C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
When closing parenthesis are part of the SQL query they have to be escaped with three carets.
(
echo insert into testconfig(testid,scenarioid,testname ^^^) values( 1,1,'asdf'^^^);
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
Use the -f parameter to pass the batch file name
C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% -f 'sql_batch_file.sql'
http://www.postgresql.org/docs/current/static/app-psql.html
-f filename
--file=filename
Use the file filename as the source of commands instead of reading commands interactively. After the file is processed, psql terminates. This is in many ways equivalent to the meta-command \i.
If filename is - (hyphen), then standard input is read until an EOF indication or \q meta-command. Note however that Readline is not used in this case (much as if -n had been specified).
if running on Linux, this is what worked for me (need to update values below with your user, db name etc)
psql "host=YOUR_HOST port=YOUR_PORT dbname=YOUR_DB_NAME user=YOUR_USER_NAME password=YOUR_PASSWORD" -f "fully_qualified_path_to_your_script.sql"
You cannot put the query on separate line, batch interpreter will assume it's another command instead of a query for psql. I believe you will need to quote it as well.
I agree with Spidey:
1] if you are passing the file with the sql use -f or --file parameter
When you want to execute several commands the best way to do that is to add parameter -f, and after that just type path to your file without any " or ' marks (relative paths works also):
psql -h %host% -p 5432 -U %user% -d %dbname% -f ..\..\folder\Data.txt
It also works in .NET Core. I need it to add basic data to my database after migrations.
Kindly refer to the documentation
1] if you are passing the file with the sql use -f or --file parameter
2] if you are passing individual command use -c or --command parameter
If you are trying the shell script
psql postgresql://$username:$password#$host/$database < /app/sql_script/script.sql

change user to oracle and run sql in a shell script

I am a root user and in a shell script I would like to change user to oracle than run a sql script, I tried following;
#!/bin/sh
portStatus=`lsof -ni:5060`
if [ ${#portStatus} -ne 0 ]
then
sudo -u oracle << EOF
/oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 #/oracle/product/102/db/GW_EP_List.sql;
EOF
else
exit
fi
it gives me following error;
./deneme2.sh: syntax error at line 12: `end of file' unexpected
Can you please let me know what might be the problem?
Thanks,
Halit
When using here documents the closing string MUST be at the beginning of the line!
Try
#!/bin/sh
portStatus=`lsof -ni:5060`
if [ ${#portStatus} -ne 0 ]
then
sudo -u oracle << EOF
/oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 #/oracle/product/102/db/GW_EP_List.sql;
EOF
else
exit
fi
You can use su. Remember get environment with su -:
COMMAND="/oracle/product/102/db/bin/sqlplus -s a51... "
su - oracle -c $COMMAND
A nice sample oracle-base site, Automating Database Startup and Shutdown on Linux Post:
case "$1" in
'start')
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start"
su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
touch /var/lock/subsys/dbora
;;
sudo -u oracle /oracle/product/102/db/bin/sqlplus -s a513s..........
You don't need EOF here. Execute your sqlplus command like above. In this case your oracle user must be a sudo user.
If oracle is a normal user
su - oracle -c "/oracle/product/102/db/bin/sqlplus -s a513s.........."
A little more about su command (From man page):
The su command is used to become another user during a login session.
Invoked without a username, su defaults to becoming the superuser.
The optional argument - may be used to provide an environment similar
to what the user would expect had the user logged in directly.
Additional arguments may be provided after the username, in which case
they are supplied to the user's login shell. In particular, an
argument of -c will cause the next argument to be treated as a command
by most command interpreters. The command will be executed by the
shell specified in /etc/passwd for the target user.