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.
Example:
yourUser=# \c newDatabaseName
You are now connected to database "newDatabaseName" as user "yourUser".
Answer from kgrittn on Stack OverflowWhen 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.
Example:
yourUser=# \c newDatabaseName
You are now connected to database "newDatabaseName" as user "yourUser".
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.
I'm new to databases and using a tutorial, I created a Postgres DB which has several schemas, tables, functions, triggers and types. With pgAdmin I can use 'CREATE script' on individual tables or functions, but how would I generate a script that will encapsulate the entire thing, i.e. running this script would create the entire database -- all of its schemas, tables, functions, triggers and types -- in one fell swoop?