I've set mongo database to current database using use currentdb.
When inserting data from local host. The data keeps entering into default database "test".
Running use <databasename> in the shell affects only the current session.
If you then connect with node.js/mongoose and use .save(), that will be a separate session.
To find out the database being used by a mongoose connection, check the name property.
To set the database name when connecting, specify the name in the URL like:
mongoose.connect('mongodb://user:pass#localhost:port/database');
According to this documentation using use mays should create a db if it isn't already created:
MongoDB use DATABASE_NAME is used to create database. The command will
create a new database, if it doesn't exist otherwise it will return
the existing database.
So why doesn't mongo use mays work?
root#server88-208-249-95:~# mongo use mays
MongoDB shell version: 2.6.11
connecting to: use
2015-09-16T22:17:20.316+0100 file [mays] doesn't exist
failed to load: mays
The 'use' command doesn't work with mongo command.
You have to open mongo shell and then use the 'use' command.
Open terminal -> enter 'mongo' to get mongo shell ->
use db_name
This will create a DB if it doesn't exists already.
A DB doesn't show up when using 'show dbs' until you create a collection in it.
Use db.createCollection("collection_name") and then use 'show dbs' and you will see your newly created DB.
You need to create at least one collection before it saves the database. Issuing a use will create it, but will not automatically save it. You can create an empty collection with db.createCollection("test"). To verify try the following commands from the mongo shell:
use mays
show dbs (mays will not show up)
db.createCollection("test")
show dbs (mays will show up)
I am trying to create a database in mongo db using command use db shorten
when i run the mongod instance it outputs this
but when try to run mongo in the separate terminal it gives error creating a database
the data/db permissions are set to 755 and i couldn't find anything related to it
If your db is called 'shorten' you should just type use shorten. It's the extra 'db' in there that's messing you up.
I am beginner to PostgreSQL.
I want to connect to another database from the query editor of Postgres - like the USE command of MySQL or MS SQL Server.
I found \c databasename by searching the Internet, but its runs only on psql. When I try it from the PostgreSQL query editor I get a syntax error.
I have to change the database by pgscripting. Does anyone know how to do it?
When you get a connection to PostgreSQL it is always to a particular database. To access a different database, you must get a new connection.
Using \c in psql closes the old connection and acquires a new one, using the specified database and/or credentials. You get a whole new back-end process and everything.
You must specify the database to use on connect; if you want to use psql for your script, you can use "\c name_database"
user_name=# CREATE DATABASE testdatabase;
user_name=# \c testdatabase
At this point you might see the following output
You are now connected to database "testdatabase" as user "user_name".
testdatabase=#
Notice how the prompt changes. Cheers, have just been hustling looking for this too, too little information on postgreSQL compared to MySQL and the rest in my view.
In pgAdmin you can also use
SET search_path TO your_db_name;
The basic problem while migrating from MySQL I faced was, I thought of the term database to be same in PostgreSQL also, but it is not. So if we are going to switch the database from our application or pgAdmin, the result would not be as expected.
As in my case, we have separate schemas (Considering PostgreSQL terminology here.) for each customer and separate admin schema. So in application, I have to switch between schemas.
For this, we can use the SET search_path command. This does switch the current schema to the specified schema name for the current session.
example:
SET search_path = different_schema_name;
This changes the current_schema to the specified schema for the session. To change it permanently, we have to make changes in postgresql.conf file.
Use this commad when first connect to psql
=# psql <databaseName> <usernamePostgresql>
set search_path = 'schema name here'
while connecting to the postgres, you have to opt for default database to connect. If you have nothing, you can use 'postgres' as default.
You can use dbeaver to connect to postgres. UI is good
PgAdmin 4, GUI Tool: Switching between databases
In the PgAdmin Browser on the left hand side, right click on the database you are willing to switch to.
Select a QueryTool from the drop down menu (or any other option that you need, I will stick with the QueryTool for now).
You will see the QueryTool in the PgAdmin window, and on top you will see the active database and the role name.
Now you can write queries against the chosen database.
You can open multiple QueryTools for multiple database, and work with them as you do with your graphical text editor.
In order to be sure that you are querying the proper database, issue the following query:
SELECT session_user, current_database();
I'm a beginner with MongoDB and I've some questions:
When I'm connected to Mongo, and i execute show dbs I see 2 databases: admin and local. What's their role? Then if I execute an insert command like db.foo.insert({"value":"mongo"}), the test database appears. Why? How can i specify a custom name for a database?
With show dbs I get the databases (somehow like show databases in sql), how can I then list the collections inside a database (I would use show tables in sql)?
When executing a command, the MongoDB tutorial always uses the db object. Is it the main object (a sort of "connection" object) that has to used for executing commands or it's something else?
Thanks!
admin and local contain various settings local to the server, like users who are authenticated to connect. Under beginner usage, you shouldn't need to worry about them at all. By default you connect to a database named test. To connect to a new database, just use databasename from the mongo command line, or mongo databasename from your OS shell.
use [database_name] and then show collections
The db object is your root handle to the currently-selected database on the mongo commmand line. The command line is really just a Javascript command line, and there are various mongodb-specific objects and functions exposed that let you do stuff. Try help() for a full listing.