Mongoexport with Query using shell script - bash

I am calling mongoexport using shell script but it keeps failing. My script is :--
mongo localhost:27038/admin -u test -p mongo123 < mongoexport.js
and my mongoexport.js file is :--
use db1
mongoexport --type=csv --fields _id -q '{"_id": "323549991" , "cat" : "ABC"}' --out report.csv
But every time I run it fails with below error :--
switched to db db1
2018-01-10T17:36:15.495+0000 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:14
bye
Now I am not sure where exactly I am messing up the syntax .
Regards.

It looks like you are connecting to your mongo. You don't need to do that in order to execute mongoexport.
You just need to connect to your host (not mongo). Take a look at the official documentation
This data resides on the MongoDB instance located on the host
mongodb1.example.net running on port 37017, which requires the
username user and the password pass.
mongoexport --host mongodb1.example.net --port 37017 --username user
--password "pass" --collection contacts --db marketing --out mdb1-examplenet.json
In your case it should look like that (Untested)
mongoexport --host localhost --port 27038 --username test --password "mongo123" --db admin --collection db1 --type=csv --fields _id -q '{"_id": "323549991" , "cat" : "ABC"}' --out report.csv
I assumed your database is called admin and your collection db1, if not replace them accordingly.

Related

pg_restore ignores .pgpass and PGPASSWORD environment variable

I want to import a backup using pg_restore without a password prompt.
I tried several options but after I run the script it will always ask for a password. pg_dump is working but not pg_restore. I can run the pg_restore command if I enter my password but I want a passwordless command or at least I don't want to enter my password because the script has to work without user interaction.
What is working for me:
PGPASSWORD=xyz pg_dump -h localhost -U user -Fc database > ~/dump_prod.pgsql
What is NOT working
1.)
PGPASSWORD=xyz pg_restore -h localhost -d database -U user -W --clean --no-owner ~/dump_prod.pgsql
2.)
pg_restore --dbname=postgresql://user:pass#localhost:5432/db -W --clean --no-owner ~/dump_prod.pgsql
3.)
touch ~/.pgpass
echo "*:*:*:*:password > ~/.pgpass
chmod 0600 ~/.pgpass
pg_restore -h localhost -d db -U user -W --clean --no-owner ~/dump_prod.pgsql
any ideas?
Regards
As per the doc, -W will prompt for a password. -w will not
-w
--no-password
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as
a .pgpass file, the connection attempt will fail. This option can be
useful in batch jobs and scripts where no user is present to enter a
password.
-W
--password
Force pg_restore to prompt for a password before connecting to a database.
The .pgpass file worked for me.
My setup:
I am restoring DB into the postgres instance running as Docker container.
The postgres instance is run using command:
docker run --name postgres_db -p 5432:5432 -e POSTGRES_PASSWORD=admin -d postgres:9.6
pg_restore is present but it is available as separate tool, outside docker setup.
The ~/.pgpass file entry looks like this:
localhost:5432:db_name:user:password
Where:
db_name is the target db name that one is going to restore.
user is the name of the user that is going to perform the restore - in my case an admin user. i.e. postgres user in the postgres instance.
password - admin user's password. i.e. admin

Role not created after using psql -c "create user...."

I'm trying to create a script that creates readonly users but while trying to create a user my database doesn't update with the new user. The terminal returns a "CREATE ROLE" and no errors.
I've tried setting -U -h -p and calling the current_user() which shows that I'm the superuser making the call.
psql \
-X \
-U $PGUSERNAME
-h $DBHOST \
-p $PORT \
-c "create user readonly with password 'welcome';" \
-d $DATABASENAME \
I'm connected to my postgres via goland and when I run the shell file which runs that psql the terminal returns a "CREATE ROLE" but the postgres isn't reflecting any new roles and when I run the query in the PostgresSQL console to find my readonly user 0 rows are found. I expected the shell script to create a user and update my local database.
As Laurenz Albe mentioned in the comment removing the \ at the end resolved this issue.

Run postgres query from another server, in bash

I have a Ubuntu server that processes documents and another that have the database (postgresql 9.3).
I ran psql -? to understand how to connect to another DB and the final command would be:
psql -U postgres -W -h 1.2.3.4 -d testdb -p 5432
It works, but I must type the password after the command is issued.
I was trying to adapt this command in a bash script:
#!/bin/bash
psql -U postgres -W mypassword -h 1.2.3.4 -d testdb -p 5432 << EOF
select * from mytable;
\q
EOF
Needless to say this is not the right command.
Also, the password does not get recognized as a password, reporting the error:
psql: warning: extra command-line argument "mypassword" ignored
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
In another server, where the script runs on the local DB, my working script is:
su - postgres -c "psql myDatabase" << EOF
select * from "myOtherTable";
\q
EOF
The question is simple, how can I write the right command for bash, to connect to another database with user/password and issue commands?
A link I tried, but password seems to not be set:
run psql query in bash
Thanks!
Try
PGPASSWORD=yourpass psql -U postgres -W -h 1.2.3.4 -d testdb -p 5432
See: https://www.postgresql.org/docs/9.3/static/libpq-envars.html
or ~/.pgpass file https://www.postgresql.org/docs/9.3/static/libpq-pgpass.html

Slimming down psql calls in database config creation

I currently have the following calls to psql to setup my PostgreSQL database. I'd like to slim this down as each time I have to input a password for every call. I haven't had much experience with psql so am not too sure about the best way to do all of the following in a shorter, more user friendly fashion.
Thanks.
# Create DB
psql -U postgres -h 127.0.0.1 -c "CREATE DATABASE \"main\" WITH OWNER maindb;"
# PostGIS setup
psql -U postgres --dbname main -h 127.0.0.1 -c "CREATE EXTENSION postgis;CREATE EXTENSION postgis_topology;CREATE EXTENSION fuzzystrmatch;CREATE EXTENSION postgis_tiger_geocoder;"
# Create users table
psql -U maindb --dbname main -h 127.0.0.1 -a -f ../lib/sql/Users.sql
# Create interests table
psql -U maindb --dbname main -h 127.0.0.1 -a -f ../lib/sql/Interests.sql
# Create user-interests table
psql -U maindb --dbname main -h 127.0.0.1 -a -f ../lib/sql/UserInterests.sql
each time I have to input a password for every call
Solve this part by using a .pgpass file. Then you can make as many calls to psql as you want.

Mongo command from unix shell with different database

I can connect to a mongo server using this command:
mongo host:port/admin --username=user --password=pass
and I get a mongo shell where I can execute any type of mongo commands after
I switch to the dev db (use dev).
I would like to execute a mongo command from the unix shell / command line
on the dev database, but I have access only to admin.
How can I do that? How can I specify the db I would like to use?
If you wish to execute just a single command, you could do it with:
mongo server:22021/my_dbname --eval "printjson(db.serverStatus())"
If you wish to execute multiple commands say residing in a file called command.js, you could do something like:
mongo server:22021/my_dbname --quiet commands.js
If you want pure shell script (SQL) style, then you could do something like:
function testMongoScript {
mongo server:22021/my_dbname <<EOF
use mydb
db.leads.findOne()
db.leads.find().count()
EOF
}
For further details, you could refer here as well.
You can use output redirection to run whatever command you need:
mongo localhost:27017 <<< $'rs.slaveOk()\nshow dbs'
or
echo $'rs.slaveOk()\nshow dbs' | mongo localhost:27017
just remember the \n between commands.
Use --authenticationDatabase to authenticate to the database where your user exists even though you are connecting to a different database:
mongo localhost:27017/dev -u user -p **** --authenticationDatabase admin
Naturally, you won't be able to do anything in the dev database your user doesn't have permissions to do.

Resources