Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the default database for psql to connect to?

While using psql, I want to change the initial database connection.

I had a database named "test" as the initial connection.

When running psql from the command line my prompt would be test=#

After deleting the "test" database, and running psql in command line, I get the following error:

psql: FATAL:  database "test" does not exist

I understand what this means, but how do I go about setting the "postgres" database as the default?

Instead of typing psql postgres each time.

like image 980
FluxEngine Avatar asked Oct 16 '12 16:10

FluxEngine


People also ask

How do I set the default database in PostgreSQL?

To create a database, you must be a superuser or have the special CREATEDB privilege. See CREATE ROLE. By default, the new database will be created by cloning the standard system database template1 . A different template can be specified by writing TEMPLATE name .

How do I change the database connection in PostgreSQL?

Switching between databases is another way of saying you are closing one connection and opening another. When you need to change between databases, you'll use the “connect” command, which is conveniently shortened to \c, followed by the database name.

How do I connect to a postgres database terminal?

Connect to PostgreSQL from the command line. At the command line in your operating system, type the following command. user@user-pc:~$ sudo -i -u postgres postgres@user-pc:~$ psql psql (9.3. 5, server 9.3.


1 Answers

You can configure the default behavior of psql -- and in fact any program using the libpq client library -- through environment variables.

In your ~/.bashrc or similar:

export PGDATABASE=postgres

The PostgreSQL documentation contains a complete list.

like image 152
willglynn Avatar answered Sep 28 '22 07:09

willglynn