psql -c passing in json data - sql

I'm having trouble passing in some json data to a psql function with the -c option. I've not written the function, and i'm not in a position to change it. The usage instructions I was given state to log in via psql and then execute
blah.function(('[{"thing":"value","other_thing":"other_value"}]'));
This works a treat, however I need to execute it as a oneliner to grab the output and manipulate with with some bash magic. I've tried all manner of escaping the json data and I get a syntax error every time.
psql -h localhost -p 5432 -U user schema -c 'SELECT blah.function(('[{"thing":"value","other_thing":"other_value"}]'));'
psql -h localhost -p 5432 -U user schema -c 'SELECT blah.function((''[{"thing":"value","other_thing":"other_value"}]''));'
psql -h localhost -p 5432 -U user schema -c 'SELECT blah.function((\'[{"thing":"value","other_thing":"other_value"}]\'));'
Can anyone enlighten me on this monday afternon, how do I pass in the JSON data?

Asked and answered. The trick was to flip the quotes round and escape the json. See below
psql -h localhost -p 5432 -U user schema -c "SELECT blah.function(('[{\"thing\":\"value\",\"other_thing\":\"other_value\"}]'));"

Related

How to create a schema in a specific database using bash script? [duplicate]

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.

Shell Script to execute SQL Queries

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

restore sql dump into remote postgres machine

How can i dump the .sql file into remote postgres machine!
Will the pg_restore will work for it to dump sql file. Please help
Code:
pg_restore -h 192.168.0.190 -p 5432 -d postgre -U postgres C:/home/mydump.sql
This worked for me
psql -h 192.168.0.190 -U postgres -p 5432 -d postgre < mydump.sql

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

Import dump/sql file into my postgresql database on Linode

I recently moved my Ruby on Rails 4 app from Heroku to Linode. Everything has been setup correctly, but I need to populate my database with a file, lets call it movies.sql
I am not very familiar with postgresql command and VPS, so having trouble getting this done. I uploaded it to Dropbox since I saw many SO posts that you can use S3/Dropbox.
I saw different commands like this (unsure how to go about it in my situation):
psql -U postgres -d testdb -f /home/you/file.sql
psql -f file.sql dbname
psql -U username -d myDataBase -a -f myInsertFile
So which is the correct one in my situation and how to run when I SSH in Linode? Thanks
You'll need to get the file onto your server or you'll need to use a different command from your terminal.
If you have the file locally, you can restore without sshing in using the psql command:
psql -h <user#ip_address_of_server> -U <database_username> -d <name_of_the_database> -f local/path/to/your/file.sql
Otherwise, the command is:
psql -U <database_username> -d <name_of_the_database> < remote/path/to/your/file.sql
-U sets the db username, -h sets the host, -d sets the name of the database, and -f tells the command you're restoring from a file.