Reputation: 266900
In MySQL, I used use database_name;
What's the psql
equivalent?
Upvotes: 1566
Views: 1271407
Reputation: 19
You can list all the databases in the psql terminal using \l
or \list
.
To select a database you can use \c dbname
or \connect dbname
.
[INFO] You cannot use this commands in pgAdmin.
Upvotes: 0
Reputation: 11
SET schema 'schemaName';
The correct translation of 'use databasename;' from mysql to postgreSQL has to consider that in postgreSQL you have also schemas. Usually your connection string has alreday defined the databasename, you have to specify the default schema.
After setting the schema name you can execute query like:
SELECT * from tableName;
without repeating the schemaName like here:
SELECT * from schemaName.tableName;
Upvotes: -5
Reputation: 1
You can just enter use [dbName]
to switch between databases without reentering your password.
Upvotes: -19
Reputation: 1104
use \c databaseName
or \connect databaseName
(Working on psql 13.3)
Upvotes: 61
Reputation: 477
You can connect using
\c dbname
If you would like to see all possible commands for POSTGRESQL or SQL follow this steps :
rails dbconsole (You will be redirected to your current ENV database)
? (For POSTGRESQL commands)
or
\h (For SQL commands)
Press Q to Exit
Upvotes: 9
Reputation: 69
Connect to database:
Method 1 : enter to db : sudo -u postgres psql
Connect to db : \c dbname
Method 2 : directly connect to db : sudo -u postgres psql -d my_database_name
Upvotes: 3
Reputation: 708
Listing and Switching Databases in PostgreSQL When you need to change between databases, you’ll use the \connect command, or \c followed by the database name as shown below:
postgres=# \connect database_name
postgres=# \c database_name
Check the database you are currently connected to.
SELECT current_database();
postgres=# \l
postgres=# \list
Upvotes: 7
Reputation: 3584
You can also connect to a database with a different ROLE as follows.
\connect DBNAME ROLENAME;
or
\c DBNAME ROLENAME;
Upvotes: 11
Reputation: 118593
In PostgreSQL, you can use the \connect
meta-command of the client tool psql:
\connect DBNAME
or in short:
\c DBNAME
Upvotes: 2362
Reputation: 35991
Though not explicitly stated in the question, the purpose is to connect to a specific schema/database.
Another option is to directly connect to the schema. Example:
sudo -u postgres psql -d my_database_name
Source from man psql
:
-d dbname
--dbname=dbname
Specifies the name of the database to connect to. This is equivalent to specifying dbname as the first non-option argument on the command line.
If this parameter contains an = sign or starts with a valid URI prefix (postgresql:// or postgres://), it is treated as a conninfo string. See Section 31.1.1, “Connection Strings”, in the
documentation for more information.
Upvotes: 22
Reputation: 8731
If you want to switch to a specific database on startup, try
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql vigneshdb;
By default, Postgres runs on the port 5432. If it runs on another, make sure to pass the port in the command line.
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p2345 vigneshdb;
By a simple alias, we can make it handy.
Create an alias in your .bashrc
or .bash_profile
function psql()
{
db=vigneshdb
if [ "$1" != ""]; then
db=$1
fi
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p5432 $1
}
Run psql
in command line, it will switch to default database; psql anotherdb
, it will switch to the db with the name in argument, on startup.
Upvotes: 5
Reputation: 169
Use below statement to switch to different databases residing inside your postgreSQL RDMS
\c databaseName
Upvotes: 11
Reputation: 379
\l
for databases
\c
DatabaseName to switch to db
\df
for procedures stored in particular database
Upvotes: 19
Reputation: 83157
Using psql's meta-command \c or \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo
(see documentation).
Example: \c MyDatabase
Note that the \c
and \connect
meta-commands are case-sensitive.
Upvotes: 14
Reputation: 239220
You can connect to a database with \c <database>
or \connect <database>
.
Upvotes: 201
Reputation: 1835
You can select the database when connecting with psql. This is handy when using it from a script:
sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test
Upvotes: 39
Reputation: 74330
At the PSQL prompt, you can do:
\connect (or \c) dbname
Upvotes: 113