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
Related
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.
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
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\"}]'));"
I want to connect to sybase than execute a file.sql
I tapped this command:
sql -U Login -P MotDePasse -S ServeurASE -d NomDeLaBase -i Script.sql -o JournalDErreur.log
I also tried
isql -U Login -P MotDePasse -S #IPserveur:Port -d NomDeLaBase -i Script.sql -o JournalDErreur.log
and I have the error :
"La commande sql n'est pas reconnue'
Can u help please?
I have installed JTDS and sql (pip install) but it doesn't work
Assuming you're trying to connect to a Sybase ASE database, and you're using the isql command line utility that comes with ASE (or the Sybase SDK), the correct isql format is:
isql -U <login> -P <password> -S <host_or_ipaddr>:<port> -D <dbname> -i <input_script> -o <output_file>
So updating your example we get:
isql -U Login -P MotDePasse -S IPserveur:Port -D NomDeLaBase -i Script.sql -o JournalDErreur.log
I can't speak for your sql example as I'm not familiar with that tool let alone its input parameters.
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.