ERROR: could not open file ""C:\Users\Zadane\Downloads\Popdata.csv" for reading: No such file or directory - csv

I am login to a database su -postgres
psql
\c postgres # connected to database
Successfully Created table population
I have a CSV file and I have successfully created table, when I am trying to use the copy command
COPY population(year,age,ethnic,sex,area,count)
FROM 'C:\Users\Zadane\Downloads\Popdata.csv' #( note my CSV file is in my download folders )
DELIMITER ‘,’
CSV HEADER**;
I get and error,
CSV HEADER;ERROR: syntax error at or near "pwd"
LINE 1: pwd
When, I remove the Delimiter from the command, I get an error
ERROR: could not open file "C:\Users\Zadane\Downloads\Popdata.csv"
for reading: No such file or directory**
\! pwd -shows displays the name:- /home/postgres
SHOW data_directory shows path:- /usr/local/postgresql/pgsql/data.
Can you let me know how to resolve the issue.or how to move .csv file to the /usr/local/postgres path, Or if there is any manually command where i can enter 70 to 80k records in a table.

Related

Sourcing one sql inside another sql file gives Error 2 : Failed to open file

I have two sql files. From one file, I am trying to source the other file and execute the commands in the other file.
Eg, Say I have two files a.sql and b.sql in the same directory.
a.sql
------
use dbName;
source b.sql
# bunch of sql statements
b.sql
------
use dbName;
# bunch of sql statments
Now, when I am trying to exexute the command as below,
mysql -u root -p dbName < /path/to/a.sql
Enter password:
ERROR at line 2: Failed to open file '/b.sql', error: 2
I have browsed through similar questions here at SO, but most of them suggest to use absolute path, however I want to use the relative path of b.sql in a.sql, as it will be migrated to prod environment and so giving absolute path won't make any sense.
Please let me know how can I fix this ?
From terminal, go to your folder :
cd /path/to/
Then, log into MYSQL terminal
mysql -uroot -p dbName
Enter password:
Then, source these files
source a.sql
Note that the first two commands are from terminal, and the last one is from MYSQL terminal.

How to insert a CSV into Mysql with Xampp shell

I am trying to upload a sql file in the database I am running following code
# mysql - u root -p idecon123 < "d:\IdeOffline\answersnew.sql"
But when I parse this it shows the help manual .
Is there any mistake in my command
Thanks
Go to http://localhost/ after starting Apache and MySQL. On the left side you will see a link for phpMyAdmin. Click that, then upload your .sql file through the browser. See http://wiki.phpmyadmin.net/pma/import for how to import files.
edit: for big files, set an uploadDir per http://docs.phpmyadmin.net/en/latest/config.html . Copy the file into that directory and you'll be able to import it directly.
You can do that by using the mysqlimport command in the command prompt.
Here is the syntax,
mysqlimport -u root -p abc#123 --ignore-lines=1 --fields-terminated-by="," --lines-terminated-by="\n" --local upam_dev C:\Users\ukrishnan\Desktop\Generated-Players\PlayerProfile.dataSet-1.csv
--ignore-line - To exclude if you have something like title in your first line of CSV
--fields-terminated-by - Can specify, how the field is terminated like , or | symbol
The filenames must correspond with the tables into which their data will be imported. If a filename contains one or more . (dots), the portion before the first dot will be assumed to be the name of the table.
If you have data in multiple files, you can specify like Profile.File-1.csv, Profile.File-2.csv. So that it will take the table name as Profile when you import these files.
These is ultimately fast when compare with the importing through PhpMyAdmin. It took me around 3-4 seconds to import a file with 100K records.

Mysql entries by mutiple line file in Shell

I can read the file, the problem is the insert of the string read in the file in mysql language. I tried to add strings contained into a file to a database (phpmyadmin).
Here is the shell :
apps="/test/list.txt"
for app in $apps; do
mysql -utoto -ptiti -h localhost test << EOF
insert into rpm (applications) values('$app');
EOF
done
The problem is that the result in the database is not the values of the file but the path of the file.
Thanks for help...!

Using mysql command line to generate CSV, can't generate it in any other directory except /tmp

I am creating csv and mysql dumps via mysql command line.
For creating mysql file I can easily created the .sql dump in my required directory
mysqldump -u"root" -p"root" dns packet --where="server_id=1 > /var/www/mydatafile/SQLData.sql
that works all okay but in case of CSV, it only creates the files in TMP folder, it can't create files in any other location
mysql -u"root" -p"" dns -e "SELECT * INTO OUTFILE '/var/www/mydatafile/my_csv.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' FROM TABLENAME";
it says
ERROR 1 (HY000) at line 1: Can't create/write to file '/var/www/mydatafile/my_csv.csv' (Errcode: 13)
I have given permission to the www directory but still it gives the same error...May I know the reason behind not creating the CSV into anyother location while SQL can be generated easily..
your directory /var/www/mydatafile/ has to be writable by the mysql user (usually mysql). You can check which user in file my.cnf (in debian/ubuntu based, located in /etc/mysql/ ).
The first command works because you generate sql instruction to stdout and redirect the output to a file, so that use the current user environment.
The second command is internal to mysql, so correct permissions are required for the mysql user.
EDIT: you can alternatively use mysqldump to generate csv with a command like this:
mysqldump -u"root" -p"root" dns packet -p -t --fields-terminated-by=, --lines-terminated-by="\r\n"

Bash script - ERROR 1 (HY000) - save tables from database

I have to repare some scripts, which stopped to work.
There is the export.sh script, which should copy and save tables from database:
DATAPUMP="/tmp/DataPump/" # Where tables have to be saved and where the old tables are saved
TABLELIST="MyShop5TableList" # Where names of tables are saved
DBSOURCE="somedb" # DB name
EXPORTDIR="../export/" # Where copies of tables have to be saved
rm ${DATAPUMP}*.export
for table in `cat ${TABLELIST}`; do
mysql -D ${DBSOURCE} -pPassword << EOF
select * into outfile "${DATAPUMP}${table}.export" from ${table};
EOF
done
cp ${DATAPUMP}* ${EXPORTDIR}
When I try to run the script export.sh, there is an error message:
rm: cannot remove '/tmp/DataPump/*.export': No such file or directory.
ERROR 1 (HY000): Can’t create/write to file "/tmp/DataPump/goods.export"(Errcode: 2)
# *and the same ERROR 1 for all other tables*
So I tryed to change the DATAPUMP directory to DATAPUMP="tmp/DataPump/" (just without the "/" at the beginning). The files was removed, but the ERROR 1 message still appears (now little bit different):
ERROR 1 (HY000): Can’t create/write to file "var/lib/mysql/tmp/DataPump/goods.export"(Errcode: 2)
# *and the same ERROR 1 for all other tables*
The path to files is changed. I'm new to bash script and I really don't know what is wrong.
PS: The script was working for a long time... We don't know what happened
I'm not 100% sure but I would bet it is permissions. Try to chown the directories which you are writing files in. Then run the script.
chown -R foo /some/path
This changes the ownsership to foo from root for the path directory at /some/path