How to import database through terminal ubuntu? - database

I want to import database in phpmyadmin mysql from terminal ubuntu. Can anybody give me command to insert such large size database?

Thanks. I got my answer.
First of all, write into termial as
$ mysql -u (username) -p (password)
Example, $ mysql -u root -p root
then, it will be like
mysql> use db_name;
Example, mysql> use my_database
then enter your source path.
mysql> source backup-file.sql;
Example, mysql> use /home/kazim_noorani/database_file.sql

Related

When migrating database from Magento to localhost I get a timeout error

I have a Magento website with a 4.33 GB database. I need to manipulate some of the data so I exported the database, then zip it but when I try to import it inside phpmyadmin on localhost I get timeout error and only 1/3 of the tables get transferred. I have edited the my.ini as well as php.ini and made the execution time 6000 but the issue is not from there. I believe it should be something with Magento that is wrong.
You need to import your database using command line.
shell> mysqldump db_name > backup-file.sql
You can load the dump file back into the server like this:
UNIX
shell> mysql db_name < backup-file.sql
The same in Windows command prompt:
mysql -p -u [user] [database] < backup-file.sql
PowerShell
C:\> cmd.exe /c "mysql -u root -p db_name < backup-file.sql"
MySQL command line
mysql> use db_name;
mysql> source backup-file.sql;

How enter to mysql from the console of Ubuntu?

I have an user called root and his password is "12233" in localhost
How I enter to mysql from the Ubuntu's console?
mysql -u root -p
And after it will ask you your password just type it in and that's it.
From the command line:
mysql -u [username] -p [password] [dbname]
for example:
mysql -u myuserid -p mypassword mainDB
This will give you access into mysql and then you can run different commands within the MySQL shell, like:
use mainDB --->select the DB you want to work on
select * from tablename; ---> get all records form the table table name
Hope this helps.

How to use/select a MYSQL database on my desktop

I have a database file on my desktop that I am trying to open through my terminal.
my database is called: myDB.db
I tried using:
mysql -u myDB.db -p
Thank you!!!
SOLVED for mysql! (Thank you Duffymo)
mysql -h localhost -u -p
>use [databaseName];
Also for sqlite you can do it this way:
sqlite3 [databaseName]
>.tables
I use the command line this way:
http://dev.mysql.com/doc/refman/5.6/en/mysql.html
mysql -h localhost -u <admin user name> -p
I'm prompted for the admin or user password.
When I get into MySQL I specify which database I want to work with:
use X;
where X is the name of the schema I'm interested in.
At that point I can execute SQL commands.

"permission denied" for pg_dump output file

i used below command to backup my database
sudo -u user_name pg_dump dbName -f /home ..../someWhere/db.sql
but it gives me this :
pg_dump: [archiver] could not open output file "/home ..../someWhere/db.sql": Permission denied
after googling this issue i find that i must backup my data under the /tmp path , but doesn't work
how can i resolve this issue ?
thanks in advance,
i am using Ubuntu 12.04 lts
It looks like your pg_dump is working fine, but it is having problems opening the output file as the sudo user. Just guessing, but, if you do the redirection as the user (presumably the one running the pg_dump command) id, that should do it, that is:
sudo -u user_name pg_dump dbName > /home ..../someWhere/db.sql
Using this technique your pg_dump will run as the postgres user (assuming that is who user_name is) and the file being written in /home... will be written using the permission of the user running the command (this user might have different permissions than the postgres user).
try pg_dump from psql command-line as below:
postgres=# \! pg_dump dbName -f /home ..../someWhere/db.sql
Do it from psql command line like below
-bash-4.1$ pg_dump -Fp dbName -f /home ..../someWhere/db.sql &
-F selects the format of the output in which p is Output a plain-text SQL script file
and & at the end will run your backup in the background.
Just in case somebody else lands here like I did, be aware that pg_dump shows this error too if the folder doesn't exist. In that case, just create the directory beforehand using mkdir -p folder_name.

Import SQL dump into PostgreSQL database

We are switching hosts and the old one provided a SQL dump of the PostgreSQL database of our site.
Now, I'm trying to set this up on a local WAMP server to test this.
The only problem is that I don't have an idea how to import this database in the PostgreSQL 9 that I have set up.
I tried pgAdmin III but I can't seem to find an 'import' function. So I just opened the SQL editor and pasted the contents of the dump there and executed it, it creates the tables but it keeps giving me errors when it tries to put the data in it.
ERROR: syntax error at or near "t"
LINE 474: t 2011-05-24 16:45:01.768633 2011-05-24 16:45:01.768633 view...
The lines:
COPY tb_abilities (active, creation, modtime, id, lang, title, description) FROM stdin;
t 2011-05-24 16:45:01.768633 2011-05-24 16:45:01.768633 view nl ...
I've also tried to do this with the command prompt but I can't find the command that I need.
If I do
psql mydatabase < C:/database/db-backup.sql;
I get the error
ERROR: syntax error at or near "psql"
LINE 1: psql mydatabase < C:/database/db-backu...
^
What's the best way to import the database?
psql databasename < data_base_dump
That's the command you are looking for.
Beware: databasename must be created before importing.
Have a look at the PostgreSQL Docs Chapter 23. Backup and Restore.
Here is the command you are looking for.
psql -h hostname -d databasename -U username -f file.sql
I believe that you want to run in psql:
\i C:/database/db-backup.sql
That worked for me:
sudo -u postgres psql db_name < 'file_path'
I'm not sure if this works for the OP's situation, but I found that running the following command in the interactive console was the most flexible solution for me:
\i 'path/to/file.sql'
Just make sure you're already connected to the correct database. This command executes all of the SQL commands in the specified file.
Works pretty well, in command line, all arguments are required, -W is for password
psql -h localhost -U user -W -d database_name -f path/to/file.sql
Just for funsies, if your dump is compressed you can do something like
gunzip -c filename.gz | psql dbname
As Jacob mentioned, the PostgreSQL docs describe all this quite well.
make sure the database you want to import to is created, then you can import the dump with
sudo -u postgres -i psql testdatabase < db-structure.sql
If you want to overwrite the whole database, first drop the database
# be sure you drop the right database !!!
#sudo -u postgres -i psql -c "drop database testdatabase;"
and then recreate it with
sudo -u postgres -i psql -c "create database testdatabase;"
Follow the steps:
Go to the psql shell
\c db_name
\i path_of_dump [eg:-C:/db_name.pgsql]
I tried many different solutions for restoring my postgres backup. I ran into permission denied problems on MacOS, no solutions seemed to work.
Here's how I got it to work:
Postgres comes with Pgadmin4. If you use macOS you can press CMD+SPACE and type pgadmin4 to run it. This will open up a browser tab in chrome.
If you run into errors getting pgadmin4 to work, try killall pgAdmin4 in your terminal, then try again.
Steps to getting pgadmin4 + backup/restore
1. Create the backup
Do this by rightclicking the database -> "backup"
2. Give the file a name.
Like test12345. Click backup. This creates a binary file dump, it's not in a .sql format
3. See where it downloaded
There should be a popup at the bottomright of your screen. Click the "more details" page to see where your backup downloaded to
4. Find the location of downloaded file
In this case, it's /users/vincenttang
5. Restore the backup from pgadmin
Assuming you did steps 1 to 4 correctly, you'll have a restore binary file. There might come a time your coworker wants to use your restore file on their local machine. Have said person go to pgadmin and restore
Do this by rightclicking the database -> "restore"
6. Select file finder
Make sure to select the file location manually, DO NOT drag and drop a file onto the uploader fields in pgadmin. Because you will run into error permissions. Instead, find the file you just created:
7. Find said file
You might have to change the filter at bottomright to "All files". Find the file thereafter, from step 4. Now hit the bottomright "Select" button to confirm
8. Restore said file
You'll see this page again, with the location of the file selected. Go ahead and restore it
9. Success
If all is good, the bottom right should popup an indicator showing a successful restore. You can navigate over to your tables to see if the data has been restored propery on each table.
10. If it wasn't successful:
Should step 9 fail, try deleting your old public schema on your database. Go to "Query Tool"
Execute this code block:
DROP SCHEMA public CASCADE; CREATE SCHEMA public;
Now try steps 5 to 9 again, it should work out
Summary
This is how I had to backup/restore my backup on Postgres, when I had error permission issues and could not log in as a superuser. Or set credentials for read/write using chmod for folders. This workflow works for a binary file dump default of "Custom" from pgadmin. I assume .sql is the same way, but I have not yet tested that
I use:
cat /home/path/to/dump/file | psql -h localhost -U <user_name> -d <db_name>
Hope this will help someone.
If you are using a file with .dump extension use:
pg_restore -h hostname -d dbname -U username filename.dump
I noticed that many examples are overcomplicated for localhost where just postgres user without password exist in many cases:
psql -d db_name -f dump.sql
You can do it in pgadmin3. Drop the schema(s) that your dump contains. Then right-click on the database and choose Restore. Then you can browse for the dump file.
I used this
psql -d dbName -U username -f /home/sample.sql
Postgresql12
from sql file:
pg_restore -d database < file.sql
from custom format file:
pg_restore -Fc database < file.dump
I had more than 100MB data, therefore I could not restore database using Pgadmin4.
I used simply postgres client, and write below command.
postgres#khan:/$ pg_restore -d database_name /home/khan/Downloads/dump.sql
It worked fine and took few seconds.You can see below link for more information.
https://www.postgresql.org/docs/8.1/app-pgrestore.html

Resources