How do I grant other users permission to write to SQLite database? - sqlite

I faced a strange error related to permissions for a SQLite database.
I type in bash
rm -f test.db
sqlite3 test.db 'CREATE TABLE t (qwe, qqq)'
chmod 666 test.db
sudo -u another-user sqlite3 test.db 'INSERT INTO t VALUES (4, 2)'
It prints
Error: unable to open database file
It prints the same if I change an owner of test.db file to another-user.
But if I try to insert the record on behalf of root or me, there is no any
error, and it's inserted successfully.
What's the troubles?
An output of getfacl test.db is the following
# file: test.db
# owner: mymedia
# group: mymedia
user::rw-
group::rw-
other::rw-
AppArmor has no profiles related to SQLite, SeLinux is disabled. I was
experimenting on Ubuntu 16.04. The version of SQLite is 3.11.0.

Here the problem seems to be not with file permissions, but with directory. Check if a directory where your database file is placed has proper permissions for another-user.

Related

How to export and import mysql database to ignore Duplicate entries for key 'PRIMARY'?

I'm attempting to write a bash script that will dump a database and then import it to a staging database. I would like the staging database to match the 'master' database.
I have the following code, however I recieve:
ERROR 1062 (23000) at line 23: Duplicate entry '1' for key 'PRIMARY'
# Dump production master database, excluding school_hosts table
mysqldump -h $MYSQL_HOST -u $MYSQL_USERNAME -p$MYSQL_PASSWORD --no-create-info --ignore-table=hcl_master.school_hosts hcl_master > hcl_master.sql
# Dump hcl staging database, for backup.
mysqldump -h $MYSQL_HOST -u $MYSQL_USERNAME -p$MYSQL_PASSWORD hclstaging_master > hclstaging_master_backup.sql
# Import dump file into staging master database
mysql -h $MYSQL_HOST -u $MYSQL_USERNAME -p$MYSQL_PASSWORD hclstaging_master < hcl_master.sql
After searching, I found that I could add --replace to the mysql command that is importing, however I recieve an error stating that:
mysql: unknown option '--replace'
Can anybody help with getting this script to work correctly? I'm unsure how I can drop the staging database before i import or how to get it to overwrite the primary key record?
Any help would be much appreciated. I am using MariaDB.
--replace is a mysqldump option that you specify when creating the dump, not something you can tell mysql when importing the dump.

Change the permission of generated hive file

I am exporting data from hive to text file saved to the local file system using the query below:
INSERT OVERWRITE LOCAL DIRECTORY '/local/file/system/directory'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
NULL DEFINED AS ''
SELECT * FROM staging_table WHERE date='2017-05-28';
The query generates the file as expected but I am having problem deleting the file because of the permission.
-rw-rw-r-- 1 hive hive 12345 May 31 13:03 000000_0
Is it possible to change the permission or owner of the file?
You will need Sudo or root access for doing this:
sudo chown -R NewOwnerName:NewGroupName /local/file/system/directory
If you are in linus just change the permission to 777 then that should be editable to any users
sudo chmod -R 777 directoryPath
or
sudo chmod 777 filePath
If you are root user you wouldn't need sudo
If you are in windows use icacls
C:\>icacls "D:\test" /grant John:(OI)(CI)F /T
According do MS documentation:
F = Full Control
CI = Container Inherit - This flag indicates that subordinate containers will inherit this ACE.
OI = Object Inherit - This flag indicates that subordinate files will inherit the ACE.
/T = Apply recursively to existing files and sub-folders. (OI and CI only apply to new files and sub-folders). Credit: comment by #AlexSpence
.

How to decrypt an encrypted sqlcipher database file on command line?

The question is simple
What I have is:
I have a database file which is encrypted using sqlcipher.
I also have the passphrase which was used to encrypt this db file
What I need is:
I need to decrypt the database file/ need a database file which is unencrypted/non encrypted/decrypted.
Download and Build sqlcipher --Skip this if sqlcipher is already installed
Pull the code from https://github.com/sqlcipher/sqlcipher in a directory (say ~/sqlcipher)
mkdir ~/bld; # Build will occur in a sibling directory
cd ~/bld; # Change to the build directory
../sqlcipher/configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto";
#configure sqlcipher
make install; # Install the build products
Decrypt the database to a plaintext database
$ cd ~/;
$ ./sqlcipher encrypted.db
sqlite> PRAGMA key = 'testkey';
sqlite> ATTACH DATABASE 'plaintext.db' AS plaintext KEY ''; -- empty key will disable encryption
sqlite> SELECT sqlcipher_export('plaintext');
sqlite> DETACH DATABASE plaintext;
Find the decrypted database at ~/plaintext.db which you can use with any sqlite browser like this.
Update : September 2015
http://sqlitebrowser.org now supports sqlcipher databases. That's neat.
Use SQliteStudio
Select SQLiteCipher and enter the password.
The database will be opened.
This shell script will decrypt a SQLCipher database called mydb.db and create one called mydb-decrypt.db. Params are $1=key, $2, path to read & write from.
#!/bin/bash
echo "Decrypting $2 using key $1"
echo "PRAGMA key='$1';select count(*) from sqlite_master;ATTACH DATABASE '$2/mydb-decrypt.db' AS plaintext KEY '';SELECT sqlcipher_export('plaintext');DETACH DATABASE plaintext;" | sqlcipher $2/mydb.db
echo "Done."
If you wanted to do this in a single command line, the guts of this are:
echo "PRAGMA key='$1';select count(*) from sqlite_master;ATTACH DATABASE '$2/mydb-decrypt.db' AS plaintext KEY '';SELECT sqlcipher_export('plaintext');DETACH DATABASE plaintext;" | sqlcipher $2/mydb.db
Building on the previous answers , I have a comprehensive answer. I have the configuration- OS X version - 10.10.4
Steps :
1. Donwload and build OpenSSL code:
$ curl -o openssl-1.0.0e.tar.gz https://www.openssl.org/source/openssl-1.0.0e.tar.gz
$ tar xzf openssl-1.0.0e.tar.gz
$ cd openssl-1.0.0e
$ ./Configure darwin64-x86_64-cc
$ make
Download and build SQLCipher code.
In another directory,
$ git clone https://github.com/sqlcipher/sqlcipher.git
$ cd sqlcipher
Change '/path/to/libcrypto.a' in the following command to your path
$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="/path/to/libcrypto.a"
$ make
Decrypt to plaintext database (As illustrated in previous post by Vinay)
$ cd ~/;
$ ./sqlcipher encrypted.db
sqlite> PRAGMA key = 'testkey';
sqlite> ATTACH DATABASE 'plaintext.db' AS plaintext KEY ''; -- empty key will disable encryption
sqlite> SELECT sqlcipher_export('plaintext');
sqlite> DETACH DATABASE plaintext;
Tis should help you decrypt the encrypted file...

Copy a heroku postgres db to a local sqlite db

I want to copy my heroku production db (postgres) to my development (sqlite).
Copying a postgres db into another postgres db is easy using heroku pg:pull. Does anyone know how to use this command to copy postgres into sqlite?
Heroku docs on pg:pull do not say how to use different types of dbs. This old article implied that it used to be possible. Setting up a local postgres db is something I'd like to avoid.
You will need do a pg_restore locally then dump the data using the -a option to dump data only.
It should look something like this:
Download a data dump.
heroku addons:add pgbackups
heroku pgbackups:capture
curl -o latest.dump `heroku pgbackups:url`
Create a temporary database.
sudo -u postgres createdb tempdb
Restore the dump to your temporary database.
sudo -u postgres pg_restore --verbose --clean --no-acl --no-owner -h localhost -d tempdb latest.dump
Dump the data in the correct format.
sudo -u postgres pg_dump --inserts -a -b tempdb > data.sql
Read dump in sqlite3.
sqlite3
> .read data.sql
This is an approximate solution. You will most likely need to make some small adjustments.
I agree with Craig Ringer that it might be worth getting postgres running locally. Hopefully this process will do the trick though!

how to create super admin in my phpMyAdmin?

I just finished my website here I used ASP.net & MySQL.
I uploaded my website file all right.
But the problem is with my database. I create my database very will and I create all my tables but the problem is that I can't execute my stored procedure?
That is because i don't have the privileges to do this operation?
The error in phpMyAdmin is:
MySQL said:
#1227 - Access denied; you need the SUPER privilege for this operation
How can I fix this?
As MySQL root:
$ mysql -u root -p # ..or, if no password has been set..
$ mysql -u root
Run this command:
GRANT SUPER ON *.* TO user#localhost;
Further reading:
http://dev.mysql.com/doc/refman/5.1/en/grant.html

Resources