Connect to Database – PostGreSQL

This is just a quick post on how to connect to particular database via the console in PostGreSQL. First, make sure you’ve logged in – just swap my_app for your role name:

chris@chris-VirtualBox:~/site$ psql postgres my_app

Password for user site: 
psql (8.4.10)
Type "help" for help.

Finally, all you have to do to connect is the following:

postgres=> \c my_app_development

psql (8.4.10)
You are now connected to database "my_app_development".
my_app =>

If you’re having trouble working out what database name you’re trying to connect to you can view a list of all the current database with the following command:

my_app => \l

 List of databases
       Name       |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges   
------------------+----------+----------+-------------+-------------+-----------------------
 postgres         | postgres | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | 
 my_app_development | site     | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | 
 my_app_production  | site     | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | 
 my_app_test        | site     | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | 
 template0        | postgres | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 |

my_app => \q -- \q to exit database list

Leave a Reply