run my sql dump in terminal - terminal

so I have can create a dump
/Applications/MAMP/Library/bin/mysqldump -u admin -p testsite > yourdump.sql
but how do I run this on another mac in terminal?

I should be as simple as running this:
mysql -u<user> -p dbName < db_backup.dump

Related

How to read .sql file by dictionary on MacOS?

I got error 2 when exec sql file by dictionary
I tried to exec mysql file on Mac OS at this dictionary: "/Users/kato/Desktop/world_mysql_script.sql"
source /Users/kato/Desktop/world_mysql_script.sql;
Then I got this error:
Failed to open file '/Users/kato/Desktop/world_mysql_script.sql', error: 2
Do you have any solution?
1.
Please try to use mysql command.
mysql -u DB_USER -p -h DB_HOST DB_NAME < /Users/kato/Desktop/world_mysql_script.sql

change user and execute one command under bash

Basically, I want to switch to user postgres and get a listing of databases. This is with a Fabric script that reads command lines from a text file one by one, executes them and then saves their output to file.
su - postgres && psql -c '\l'
When I do this under bash directly:
(ssha)root ~$su - postgres && psql -c '\l'
postgres#localvm:~$
I saw a related question, linux - Executing multiple commands under as another username within a file in BASH shell, but that wouldn't work with my 1-line-per-command scheme and I don't need a full script, just 1 command.
You can use su -c:
su - postgres -c "psql -c '\l'"
Though often you'll also have sudo, which is more robust and easier to use:
sudo -u postgres psql -c '\l'

Importing a database dump -- PSQL

I am trying to import an old .gz database dump into my database using the terminal. It is a Postgresql environment.
This is what i am doing:
psql test < 052710_1.gz
Responce:
ERROR: syntax error at or near "test"
LINE 1: test
^
I also tried:
psql --dbname test < --file 052710_1.gz
psql -d test -U postgres -f 052710_1.gz
And they both gave me the same error.
I have tried using the .exe on the end of psql and it has the same issue.
I am running Postgresql 10.1
For the case of version 10.1
try using the following command
pg_restore -d test < 052710_1.gz
Hope this works.
Please follow the commands. Hope it will works.
sudo psql -U postgres
 create database temp_databse;
 exit
Execute following command
 
psql -U postgres temp_database < extracted_database_file_name
 Above command will restore database...

Bash / Docker exec: file redirection from inside a container

I can't figure out how to read content of a file from a Docker container. I want to execute content of a SQL file into my PGSQL container. I tried:
docker exec -it app_pgsql psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql
My application is mounted in /usr/src/app. But I got an error:
bash: /usr/src/app/migrations/*.sql: No such file or directory
It seems that Bash interprets this path as an host path, not a guest one. Indeed, executing the command in two times works perfectly:
docker exec -it app_pgsql
psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql
I think that's more a Bash issue than a Docker one, but I'm still stuck! :)
Try and use a shell to execute that command
sh -c 'psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql'
The full command would be:
docker exec -it app_pgsql sh -c 'psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql'
try with sh -c "your long command"
Also working when piping backup to the mysql command:
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
You can use the database client in order to connect to you container and redirect the database file, then you can perform the restore.
Here is an example with MySQL: a container running MySQL, using the host network stack. Since that the container is using the host network stack (if you don't have any restriction on your MySQL or whatever database), you can connect via localhost and performing the commands transparently
mysql -h 127.0.0.1 -u user -pyour_passwd database_name < db_backup.sql
You can do the same with PostgresSQL (Restore a postgres backup file using the command line?):
pg_restore --host 127.0.0.1 --port 5432 --username "postgres" --dbname "mydatabase" --no-password --clean "/home/dinesh/db/mydb.backup"
Seems like that "docker exec" does not support input redirection.. I will verify this and maybe open an issue for Docker Community at GitHub, if it is applicable.

PostgreSQL Command fom bash script

I work with PostgreSQL 9.2 and bash script. I have a problem.
My DB is TestDB ,
Table name is config
and my bash code is
sudo -u postgres psql -c "select count(*) from public.config where var_name='url'"
result is
ERROR: relation "public.config" does not exist
Can anybody help me?
sudo -u postgres psql TestDB -c "select count(*) from config where var_name='url'
The Database was missing, so you executed the command into postgres db where you probably don't have that table ;-)

Resources