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 Overflow
🌐
Reddit
reddit.com › r/postgresql › create script for entire db, all functions, types, tables, triggers... animal style?
r/PostgreSQL on Reddit: CREATE script for entire db, all functions, types, tables, triggers... animal style?
December 8, 2020 -

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?

Discussions

postgresql - Create Postgres database using batch file with [template],[encoding],[owner] and a .sql file - Stack Overflow
Explore Stack Internal ... I want to create a Postgres database using a batch file. Now the normal way of doing this is the following: "C:\Program Files\PostgreSQL\9.0\bin\createdb.exe" -U Myadmin MydatAbseName · This script above creates a database with the default database parameters. More on stackoverflow.com
🌐 stackoverflow.com
Use PostgreSQL database in SQL script - Stack Overflow
I'm creating 3 databases from a single file: CREATE DATABASE "products" WITH OWNER = postgres ENCODING = "UTF8" CONNECTION LIMIT = -1 IS_TEMPLATE = False; CREATE DATA... More on stackoverflow.com
🌐 stackoverflow.com
sql - Create database, tables etc in one script postgresql - Stack Overflow
Find centralized, trusted content ... you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... But it doesn't seem to work in PostgreSQL. Error message: "error near GO". I dont get it, how to create scripts in Postgresql? ... When i remove "GO" and replace it by ";" I've got an error. Message: "CREATE DATABASE cannot be ... More on stackoverflow.com
🌐 stackoverflow.com
plpgsql - PostgreSQL: How to switch database inside cursor script - Database Administrators Stack Exchange
I want to write function for postgresql 10 to grant read-only access to on all databases for the specified user (considerably should be run from superuser) But I have difficulties in database switching part. I try to switch the database inside a cursor but I get an error I can't understand. How can I correct my script... More on dba.stackexchange.com
🌐 dba.stackexchange.com
December 26, 2021
🌐
Particular
docs.particular.net › persistence › sql › postgresql-scripts
PostgreSQL Scripts • Sql Persistence • Particular Docs
2 weeks ago - Used by ISubscriptionStorage. ... insert into "public"."EndpointNameSubscriptionData" ( "Id", "Subscriber", "MessageType", "Endpoint", "PersistenceVersion" ) values ( concat(@Subscriber, @MessageType), @Subscriber, @MessageType, @Endpoint, @PersistenceVersion ) on conflict ("Id") do update set "Endpoint" = coalesce(@Endpoint, "public"."EndpointNameSubscriptionData"."Endpoint"), "PersistenceVersion" = @PersistenceVersion
🌐
PostgreSQL
postgresql.org › message-id › m3mxujgnsl.fsf@passepartout.tim-landscheidt.de
PostgreSQL: Re: Equivalent to "use database" in postgre
June 25, 2010 - > i need to know how to do a simple script that create a database,the y > select it (in other languajes using USE) and after create tables with this > database. > How can I say "use name_database" on postgre sql?
🌐
CommandPrompt Inc.
commandprompt.com › education › postgresql-listing-and-switching-databases-in-psql
PostgreSQL: Listing and Switching Databases in psql — CommandPrompt Inc.
February 1, 2023 - Example 2: Switching Database Using “\connect” Command · Alternatively, you can use the “\connect” command followed by the database name to switch the databases in psql: ... The output signifies that the “\connect” command successfully ...
Address   2950 Newmarket ST STE 101 - 231, 98226, Bellingham
🌐
DEV Community
dev.to › kylefoo › create-database-and-user-with-psql-script-for-postgres-3bo4
Create database and user with psql script for Postgres - DEV Community
April 3, 2021 - After installing/upgrading postgres via Postgres.app you can launch psql with the command on terminal: ... Then, enter \du command to print the list of roles to check your newly created role - kyle. ... Also, enter \du to check your newly assigned role attributes (permission). ... Then, enter \l command to print the list of database to check your newly created database. ... You are done with creating database kylefoo with owner kyle assigned as owner. You can use the credentials in the database.yml on your rails application.
Find elsewhere
🌐
Micro Focus
microfocus.com › documentation › enterprise-developer › ed60 › ED-Eclipse › GUID-9B6FC151-7B66-4290-B2C0-DB770253A5D8.html
Create PostgreSQL Datastores Using Script Files
dbfhadmin -createdb -provider:pg -type:datastore -file:<script-name.sql> -user:<user> where <script-name.sql> is the name of the script file that was created in an earlier step, and <user> is a valid user name for the database. It is also assumed that an entry for that user name has been created ...
🌐
PostgreSQL
postgresql.org › docs › current › app-psql.html
PostgreSQL: Documentation: 18: psql
May 14, 2026 - An alternative way to specify connection parameters is in a conninfo string or a URI, which is used instead of a database name. This mechanism give you very wide control over the connection. For example: $ psql "service=myservice sslmode=require" $ psql postgresql://dbmaster:5433/mydb?sslmode=require
🌐
IBM
ibm.com › docs › en › baw › 24.0.x
Running the generated PostgreSQL database scripts - IBM Documentation
January 29, 2026 - For example, use the following commands to run the scripts manually for the Content database configuration: \c cpedb SET ROLE dbUser; \i /cpedb/createSchema_Standard.sql ... You must now run the bootstrap utility to load configuration data for ...
🌐
Stack Overflow
stackoverflow.com › questions › 74742233 › use-postgresql-database-in-sql-script
Use PostgreSQL database in SQL script - Stack Overflow
CREATE DATABASE "products" WITH OWNER = postgres ENCODING = "UTF8" CONNECTION LIMIT = -1 IS_TEMPLATE = False; CREATE DATABASE "accounts" WITH OWNER = postgres ENCODING = "UTF8" CONNECTION LIMIT = -1 IS_TEMPLATE = False; CREATE TYPE role as ENUM ...
🌐
Medium
medium.com › @dmitry.romanoff › postgresql-script-to-connect-to-db-and-get-tables-their-records-number-and-size-2ec3a0fe96bb
PostgreSQL: script to connect to DB, and get tables, their records number and size. | by Dmitry Romanoff | Medium
November 11, 2023 - #!/bin/bash ########################################## # # check_size_db_objects_and_rowsnum.sh # # This script connects to the DB and # retrives the info # about tables number of records and size. # # Date: 11-Nov-2022 # # Author: Dmitry # ########################################## helpFunction() { echo "" echo "Usage: $0 -h db_hostname -p port -U db_username -d db_name" echo -e "\t-h Postgres db hostname" echo -e "\t-p Postgers db port" echo -e "\t-U Postgres db username" echo -e "\t-d Postgres db name" echo -e " " echo -e "Example how to run: $0 -h localhost -p 5432 -U my_db_user -d my_db_name " echo -e " " exit 1 # Exit script after printing help } while getopts "h:p:U:d:" opt do case "$opt" in h ) inpHost="$OPTARG" ;; p ) inpPort="$OPTARG" ;; U ) inpDBUser="$OPTARG" ;; d ) inpDBName="$OPTARG" ;; ?
🌐
MathWorks
mathworks.com › database toolbox › relational databases › postgresql native interface
executeSQLScript - Execute SQL script on PostgreSQL database - MATLAB
Then, run two SQL SELECT statements from the SQL script file compare_sales.sql, import the results, and perform simple sales data analysis. The file contains two SQL queries: the first retrieves sales of products from US suppliers, and the second retrieves sales of products from foreign suppliers. Create a PostgreSQL native interface database connection to a PostgreSQL database using the data source name, user name, and password.
🌐
TutorialsPoint
tutorialspoint.com › postgresql › postgresql_select_database.htm
PostgreSQL - SELECT Database
Following is a simple example − · psql -h localhost -p 5432 -U postgress testdb Password for user postgress: **** psql (9.2.4) Type "help" for help. You are now connected to database "testdb" as user "postgres". testdb=# You are now logged ...
🌐
Medium
medium.com › analytics-vidhya › how-to-create-a-postgres-database-394b362abfbe
How to create a Postgres database | by AlexV | Analytics Vidhya | Medium
January 3, 2021 - Then we need to connect to the database to create the TYPE’s colours, sexes and sizes. We need to create the extension “uuid-ossp” so that we can use the function uuid_generate_v4() to auto-generate our clothes_stock id’s. Then we create the “clothes_stock” table and insert the first item. You can run this script with the following in your Terminal:
🌐
Yugabyte
yugabyte.com › blog › generate-sql-script-postgresql
Generate SQL Script in PostgreSQL
February 26, 2026 - PostgreSQL has many features that can be helpful here. All SQL databases provide their metadata as relational views on the catalog (i.e. database dictionary). That is not a coincidence. Codd’s rule number 4 mandates that a dynamic online catalog must be based on the relational model. Therefore, it’s logical to use SQL queries to generate DDL dynamically from the database metadata and execute the output.