Unable to run psql command from within a BASH script - macos

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.

Related

How do I automate oracle DR with script run through putty

So I am Trying to sync my oracle production server to oracle dr server there is a set of command that I have to run each time to remove transport and apply lag. so fistly i have to login to putty
so to automate that I created an executable
putty.sh
which reads
`putty.exe -ssh servername -l username(oracle) -pw password -m C:/auotomation/dgmgrl.sh(this is a file where my script is safe that I want to run)
the command in dgmgrl.sh file are
#echo off
dgmgrl
"and so on"
i am getting error that bash: line 3: dgmgrl: command not found
I am not getting this error when typing this command manualy
I am getting errors while running dgmgrl;
I am also getting errors while running commands like sqlplus /nolog
but commands like LS and CD are running fine

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.

heroku pg: pull not fetching tables from heroku database

I'm trying to pull a heroku database to my local Windows computer by using heroku bash command
heroku pg:pull HEROKU_POSTGRESQL_COLOR mydatabase --app appname,
when I running above command I get the following error:
'env' is not recognized as an internal or external command, operable program or batch file.!
But local database 'mydatabase' is created, but without any tables.
My heroku app's database has a table in it, but it is not getting pulled to my local database.
Help me to solve it.
a couple of things:
1.When there is an error such as "'env' is not recognized as an internal or external command, operable program or batch file" it means that the system is trying to execute a command named env. This has nothing to do at all with setting up your environment variables.
Env is not a command in windows, but in unix. I understand that you have a windows machine though. What you can do is run "git bash". (You could get it by itself but it comes with Heroku's CLI).
This gives you a unix-like environment where the "env" command is supported, and then you can run the actual heroku pg:pull command.
2.If that still doesn't work, there is a workaround which works,without installing anything extra. Actually this is based on a ticket which I submitted to Heroku so I'm just going to quote their response:
"The pg:push command is just a wrapper around pg_dump and pg_restore commands. Due to the bug you encountered, it sounds like we should go ahead and do things manually. Run these using cmd.exe (The Command Prompt application you first reported the bug). First grab the connection string from your heroku application config vars.
heroku config:get DATABASE_URL
Then you want to pick out the username / hostname / databasename parts from the connection string, ie: postgres:// username : password # hostname : port / databasename. Use those variables in the following command and paste in the password when prompted for one. This will dump the contents of your heroku database for a local file.
pg_dump --verbose -F c -Z 0 -U username -h hostname -p port databasename > heroku.dump
Next you will load this file into your local database. One thing that the CLI does before running this command is to check and make sure the target database is empty, because running this against a database with real data is something you want to avoid so be careful with pg_restore. When running this manually you run the risk of mangling your data without the CLI check, so you may want to manually verify that the target database is empty first.
pg_restore --verbose --no-acl --no-owner -h localhost -p 5432 -d mydb2 < heroku.dump
I am sorry this is not a better experience, I hope this will help you make progress. We are in the process of rewriting our pg commands so that they work better on all platforms including windows, but there is no solid timeline for when this will be completed."
For taking backup like dump file in heroku firstly you need the backups addon, installing..
$heroku addons:add pgbackups
Then running below command will give you dump file in the name of latest
$ heroku pgbackups:capture
$ curl -o latest.dump `heroku pgbackups:url`
or
wget "`heroku pgbackups:url --app app-name`" -O backup.dump
Edited:(After chatting with user,)
Problem: 'env' is not recognized as an internal or external command, operable program or batch file.!
I suspected that one of the PATH variable to particular program is messed up. You can double click and check that in WINDOWS\system32 folder.
Ok so How to edit it:
My Computer > Advanced > Environment Variables
Then choose PATH and click edit button

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.

Running Sybase SQL commands from DOS/Windows batch file

Is there any way I can run Sybase SQL commands from command prompt. I need to write a batch file which runs an SQL query on machine as a fix for a bug.
Use ISQL. See http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.utility/html/utility/utility10.htm for the complete reference.
Specifically, you're looking for commands like this:
ISQL -S server -D database -U user -P password
This will launch ISQL with the indicated settings, and from there you can run SQL statements against the database.

Resources