PostgreSQL Table export automation - postgresql

I'm trying to export the table overnight using the Windows Task scheduler, if i manually login to command prompt and execute the below three commands one-by-one it works fine. csv file will be created on the destination directory. If I try to create a bat script and try to execute the same, it stops after executing first line(i need to manually type the second line), is there a way to automate it please?
psql -U nrgadmin -d enwdb
\copy nrgcore.bookmarks to 'D:\Bookmarks_Table\Bookmarks.csv' csv;
\q

Related

How do you run code on a remote postgres database on heroku from .bat file

If I remote into a postgres database on heroku with heroku pg:psql -a appname I can wait a few seconds for it to connect, then run \copy (SELECT * FROM users) TO users.csv CSV DELIMITER ',' HEADER to extract a table from the remote database to my desktop.
However, if I put these two lines of code into a .bat file and run it, it opens cmd.exe and connects to the postgres database (as expected), but then nothing happens (no more code is executed)
How do you construct the .bat file so that it runs the first line (to connect to the remote database), then executes the subsequent lines inside the remotely connected database?
You would have to pass the command to psql with the -c switch.
psql -c "\copy (SELECT * FROM users) TO users.csv CSV DELIMITER ',' HEADER"
If you need to execute more commands it will be easier to put them in a seperate file and pass that to psql with the -f switch.

How to export all tables in a PostgreSQL database to csv files?

I went to the psql commandline mode and entered the correct database and I can list all the tables.
Now, I tried the following commands:
copy some_table_name1 to '/var/lib/pgsql/csv_exports/some_table_name1.csv' csv header
copy some_table_name2 to '/var/lib/pgsql/csv_exports/some_table_name2.csv' csv header
And so on...
There was no error messages or anything after the commands and I used tab-button to ensure that I was always referring to correct table names.
After doing this to all the tables I went to the directory and there were no files at all.
Am I doing something wrong?
EDIT: I should clarify that I was looking to that directory, by using putty and WINSCP, on the server machine. The same where I ran the psql commands in.
The files are written to that directory on the server machine, not the client.
Use COPY ... TO STDOUT to send the data to the client.
Using psql in the console you can use the following command to get your data in the client machine:
$ psql yourdb -c "COPY yourtable TO STDOUT DELIMITER ',' CSV HEADER" > output.csv
If you're wondering about how to do the other way around (import), take a look at this question.
Assuming that what you really want to do is to output the file to someplace on your local machine (ie your developer workstation), I suggest that you use the "\copy" command, instead of the "COPY" command.
psql -c "\copy (SELECT * FROM account) to '/tmp/account.csv' with csv;"
or
psql -c "\copy account TO '/tmp/account.csv' DELIMITER ',' CSV HEADER;
Otherwise, unless you explicitly redirect the command to stdout, as #LaurenzAlbe suggested, the "COPY"command will check and see if you are authorized to write the files out to the actual database server. Normally, this is not the behavior that you and, and it requires accesses and permissions greater than most developers have.

Is there a way to use 'COPY' command(PostgreSQL) in windows cli?

Trying to use 'copy' command of PostgreSQL in windows cli command:
COPY myTable FROM value.txt (DELIMITER('|'));
I can't find 'copy' executable file in bin directory.
Can you let me know how can I run 'copy' command in cli?
Added:
My windows application is going to use the 'Copy' feature.
Need to run it directly from Application.
Thanks in advance.
I could manage to see my required result with following approach.
psql.exe -f copy.sql -p 5433 -U user -s postgres
copy.sql
\copy TARGET_TABLE FROM source.txt (DELIMITER('#'));
You have the right command (COPY), but you need to begin an interactive session with Postgres if you want use the COPY command from the Windows command line.
psql -U username yourdb
This should leave you at a prompt looking like the following:
yourdb=#
Now you can use the COPY command and it should work:
COPY myTable FROM value.txt (DELIMITER('|'))
The problem you were having is that COPY is not a Windows executable program, it is a Postgres command which is only understood by Postgres.
For loading Json use the below command-
\COPY myTable (data) FROM '~specifypath/data.json';
data is the column where you want to push the json

How to run postgres sql script from another script?

I have a bunch of SQL scripts that create tables in the database. Each table is located in a separate file so that editing them is much easier.
I wanted to prepare a single SQL script that will create the full schema, create tables, insert test data and generate sequences for the tables.
I was able to do such thing for oracle database but I am having problems with postgres.
The thing is - I do not know how to run the table creating script from another script.
In oracle I do it using the following syntax:
##'path of the script related to the path of the currently running sql file'
And everything works like a charm.
In postgres I was trying to search for something alike and found this:
\ir 'relative path to the file'
Unfortunately when I run my main script I get the message:
No such file or directory.
The example call is here:
\ir './tables/map_user_groups.sql'
I use Postgres 9.3. I tried to run the script using psql:
psql -U postgres -h localhost -d postgres < "path to my main sql file"
The file executes fine except for the calling of those other scripts.
Does anybody know how to solve the problem ?
If something in the question is unclear - just let me know :)
Based on the answer It is possible to reference another SQL file from SQL script, on PostgreSQL, you can include another SQL's files just using the \i syntax. I just tested and is working good on PostgreSQL 9.6:
\i other_script.sql
SELECT * FROM table_1;
SELECT * FROM table_2;
By the #wildplasser comment:
psql -U postgres -h localhost -d postgres < "path to my main sql file"
From psql's perspective the main_sql file is just stdin, and stdin has no "filename". Use -f filename to submit a file with a name:
psql -U postgres -h localhost -d postgres -f filename.sql
How to run postgres sql script from another script?
Seems the same question as: How to import external sql scripts from a sql script in PostgreSQL?

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.