You can put in your file something like:

\c first_db_name
select * from t; --- your sql
\c second_db_name
select * from t; --- your sql
...
Answer from panos on Stack Overflow
🌐
PostgreSQL
postgresql.org › message-id › m3mxujgnsl.fsf@passepartout.tim-landscheidt.de
PostgreSQL: Re: Equivalent to "use database" in postgre
June 25, 2010 - You must specify the database to use on connect; if you want to use psql for your script, you can use "\c name_database" à la: | CREATE DATABASE testdatabase; | \c testdatabase | CREATE TABLE testtable (testcolumn int);
Discussions

CREATE script for entire db, all functions, types, tables, triggers... animal style?
pg_dump is the tool for that, targeting one database within the "cluster" (terrible postgres terminology--that's just one instance of postgres running, which can have multiple databases). it's very robust and configurable, check the options. https://www.postgresql.org/docs/current/app-pgdump.html pg_dumpall is a wrapper around pg_dump that will do this for the entire cluster (all the databases + the users/roles) More on reddit.com
🌐 r/PostgreSQL
7
6
December 8, 2020
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
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
postgresql - Create a database and make it "current" - Database Administrators Stack Exchange
At the moment it's run with \i . It goes like · // create user, give him some priveleges CREATE DATABASE my_database; CREATE TABLE my_table ( id UUID primary key, data json ); But my_table is created in postgres database, not in my_database. How can I specify that my_table should be created in my_database? I use postgresql ... More on dba.stackexchange.com
🌐 dba.stackexchange.com
October 28, 2018
🌐
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?

🌐
Stack Overflow
stackoverflow.com › questions › 74742233 › use-postgresql-database-in-sql-script
Use PostgreSQL database in SQL script - Stack Overflow
In PostgreSQL you have to specify the database when you open a connection to the server. You can not switch to a different database using SQL. From the documentation of the low-level API for opening connections: An application program can have ...
🌐
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:
🌐
CommandPrompt Inc.
commandprompt.com › education › postgresql-listing-and-switching-databases-in-psql
PostgreSQL: Listing and Switching Databases in psql — CommandPrompt Inc.
February 1, 2023 - “\List”, “\l”, and “pg_database” is used in SQL Shell to get the database list. While to switch databases in SQL Shell, the “\c” and “\connect” commands are us…
Address   2950 Newmarket ST STE 101 - 231, 98226, Bellingham
Find elsewhere
🌐
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.
🌐
Micro Focus
microfocus.com › documentation › enterprise-developer › ed60 › ED-Eclipse › GUID-9B6FC151-7B66-4290-B2C0-DB770253A5D8.html
Create PostgreSQL Datastores Using Script Files
To create the database script file, on the command line, enter the following command: dbfhadmin -script -type:datastore -provider:pg -name:<datastore-name> -file:<script-name.sql> where <script-name.sql> is the name of the script file to be created. Note: If you omit the -file option, the default ...
🌐
Datensen
datensen.com › postgresql › generate-postgresql-sql-script.html
How to generate SQL script for PostgreSQL
Luna Modeler database modeling and SQL script generation for PostgreSQL. Automatically generate scripts from your PostgreSQL database designs.
🌐
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 the IBM® Business Automation Workflow applications into the Process database. This data is required for the applications to run correctly. If your PostgreSQL ...
🌐
PostgreSQL
postgresql.org › docs › current › app-psql.html
PostgreSQL: Documentation: 18: psql
May 14, 2026 - However, when invoked from a script, \ir interprets file names relative to the directory in which the script is located, rather than the current working directory. ... List the databases in the server and show their names, owners, character set encodings, and access privileges.
🌐
Zachary Betz
zwbetz.com › connect-to-a-postgresql-database-and-run-a-query-from-a-bash-script
Connect to a Postgres Database and Run a Query From a Bash Script | Zachary Betz
June 7, 2019 - PGHOST='localhost' PGPORT='5432' PGDATABASE='some_database' PGUSER='some_user' PGPASSWORD='some_password' ... #!/usr/bin/env bash # If psql is not available, then exit if ! command -v psql > /dev/null ; then echo "This script requires psql to be installed and on your PATH.
🌐
TutorialsPoint
tutorialspoint.com › postgresql › postgresql_select_database.htm
PostgreSQL - SELECT Database
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 into PostgreSQL testdb and ready to execute your commands inside testdb.
🌐
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.