Using shell script to grab data from Heroku Postgres database - postgresql

I'm a newbie at this. I want to write a script that I can execute from command line to run a query on a Heroku-hosted PostgreSQL database.
Right I have a script script.sh with executable permissions that looks like:
echo "Starting pull from postgres ..."
heroku pg:psql <db> --app <app-name>
\copy (<query>) to 'file.csv' WITH CSV
\q
echo "Done!"
The echo and heroku ... commands runs fine, however, once Heroku launches, the script no longer injects the commands. Only after I manually close out the Heroku app does it inject the last three lines.
I understand that this is a bash script that isn't intended to input postgreSQL commands once Heroku is open, but is there a way to do this?
I get the sense that it might involve connecting to Heroku and submitting the query in one line -- I searched around Heroku's documentation but didn't see anything that would be helpful.

You can use the --command flag to pass SQL commands to heroku pg:psql along with the server-based COPY and then redirect the output to a file:
echo "Starting pull from postgres ..."
heroku pg:psql <db> --app <app-name> --command "COPY (<query>) TO STDOUT WITH CSV" > file.csv
echo "Done!"

Related

PostgreSql Equivalent of sqlplus -S username/password \#lock?

What will be Postgres equivalent of following:
sqlplus -S username/password \#lock.
Also what does #lock means here?
I don't know PostgreSQL, but - as of Oracle, here you are:
That command means that
you'll connect to Oracle's command line tool called SQL*Plus (executable name is sqlplus)
in silent mode (that's what -s does)
providing username and password
and execute .SQL script whose name is lock (I have no idea what it does; you'll have to open it in any text editor and have a look)
Now, how someone establishes connection to PostgreSQL and runs a .SQL script, that's something I wouldn't know, but - reading online documentation - it might be
psql -U username -d database_name -a -f lock
According to the explanations in the comments and the other answer, the equivalent in PostgreSQL should be
psql 'user=username password=password dbname=mydatabase' -f lock

Deploy postgres via jenkins - continuous integration/deployment

I got my database dump (tables, functions, triggers etc) in *.sql files.
At this moment I am deploying them via jenkins, by passing execute shell command:
sudo -u postgres psql -d my_db < /[path_to_my_file].sql
The problem is, that if something is wrong in my sql file, build finishes as SUCCESS. I would like to got information immediately if something fails, without looking into log and checking if every command executed succesfully.
Is it possible (and how if the answer is 'yes') to deploy postgres database via jenkins other way?
I changed my execution command to:
sudo -u postgres psql -v ON_ERROR_STOP=1 -d my_db < [path_to_file].sql
Make sure you have set
set -e
Before running the command.
If that does not work, I'd look at the return code from the command above. That can be done by running
echo $?
right after the command.
If that gives you a zero when it fails it's postgres fault (sice it should return with something else than 0 on fail).
Perhaps there is a postgres flag to fail on wrong input.
EDIT:
-v ON_ERROR_STOP=1
As a flag to postgres should make postgres fail on errors

How to enable quiet mode for Postgres commands on Heroku

When using the psql command line utility on my local machine, I have the option to use the -q or --quiet switch to tell Postgres to do it's work quietly - i.e. it won't print every single INSERT statement to the console if you're doing a large import.
Here's an example of how I'm using it:
psql -q -d <SOME_DATABASE> -f <SOME_SQL_FILE>
However, when using the pg:psql command line utility in Heroku, that option doesn't seem to be available. So I'm currently having to use it like so:
heroku pg:psql DATABASE -a <SOME_HEROKU_APP> < <SOME_SQL_FILE>
which produces a lot of output to my console (hundreds of thousands of lines), because of the large size of the SQL file I'm importing. Whenever I try to use the -q or --quiet option, something like this:
heroku pg:psql DATABASE -q -a <SOME_HEROKU_APP> < <SOME_SQL_FILE>
it'll throw an error saying that -q is not a valid option.
Is there some way to enable quiet mode when running Postgres commands in Heroku?
heroku pg:psql is just a wrapper onto your local psql binary (https://github.com/heroku/heroku/blob/master/lib/heroku/command/pg.rb#L151)
So, given this - you are able to do:
psql `heroku config:get DATABASE_URL -a <yourappname>`
to get a psql connection and consequently pass -q other options accordingly.

PostgreSQL - read an SQL file into a PostgreSQL database from the commandline

I use Ruby to generate a bunch of SQL commands, and store this into a file.
I then login to my PostgreSQL database. Then I do something like:
\i /tmp/bla.sql
And this populates my database.
This all works fine as it is, no problem here.
I dislike the manual part where I have to use \i, though (because I need this to work in a cron job eventually, and I think commands like \i are only available when you are directly in the interactive psql prompt).
So my question now is:
Is it possible to use a psql command from the command line that directly will start to read in an external file?
You can directly use the psql command as shown below.
Works for me with Ubuntu and Mint. On Windows it should be quite the same...
psql -U user -d database -f filepath
Example:
psql -U postgres -d testdb -f /home/you/file.sql
For more information take a lock at the official documentation: http://www.postgresql.org/docs/current/static/app-psql.html
When you try to execute an sql file using cron, you will also need to set the environment - database name, password etc. This is a short shell script snippet that does it all
source /var/lib/pgsql/scripts/.pgenv
echo $PATH
psql << AAA
select current_date;
select sp_pg_myprocedure(current_date);
AAA
In .pgenv, you set the values such as
export PGPORT=<yourport>
export PGHOST=<yourhost>
export PGDATA=<yourdatadir>
Also have a .pgpass file so that the password is supplied.
http://www.postgresql.org/docs/current/static/libpq-pgpass.html
Replace the part where SELECT is being done with whatever you want to do, or do it as #Kuchi has shown.

Unable to restore the postgresql data through command prompt

I am trying to restore the postgres sql data from a file . I am trying to do so but it is not importing .
Here is the command which i am using:
postgres-# psql -hlocalhost -p5432 -u postgres -d test -f C:/wamp/www/test/database_backups/backup004.sql
Please help me what I am doing wrong .
I am using windows and the above command does not throws any error but it does not import data.
Regards
Surjan
The only immediate thing I can see there is the capitilsation of -u for username (should be -U).
Correction: You're typing the command line into the psql shell.
You should exit to the CMD.EXE shell, and try the command there. With the correct capitalisation of -U, by the way.
OR, use this to replay the script into that psql shell:
\i C:/wamp/www/test/database_backups/backup004.sql
The forward slashes don't cause a problem on my Windows machine.