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 OverflowYou 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
...
Are you piping these commands through the psql command? If so, \c databasename is what you want.
psql documentation
CREATE script for entire db, all functions, types, tables, triggers... animal style?
Use PostgreSQL database in SQL script - Stack Overflow
plpgsql - PostgreSQL: How to switch database inside cursor script - Database Administrators Stack Exchange
postgresql - Create a database and make it "current" - Database Administrators Stack Exchange
Videos
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".
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?
In 'psql', you can change the database you are connected to by using the \c metacommand. So it would look like this:
CREATE DATABASE my_database;
\c my_database
CREATE TABLE my_table (
id UUID primary key,
data json
);
This works from '\i' included files. When the \i file is done, you will be left in the new database unless you do something to change back to the old one.
In Postgres, you always connect to one specific database. Your entire session addresses only that one database (a.k.a. catalog).
So you cannot create and define a new database/catalog in one session. Creating the empty database and then filling it needs to be done in two separate phases using two different connections.
Phase One: Connect to an existing database such as the usual default database named postgres. Execute your CREATE DATABASE statement.
Phase Two: Get a new connection to this new database. Add your table definitions. Populate your rows.
I suggest using a database migration tool such as Flyway or Liquibase for handling and executing SQL scripts to do the Phase Two work.
Try this one:
#!/bin/bash
psql -U postgres -d database_name -c "SELECT c_defaults FROM user_info WHERE c_uid = 'testuser'"
Or using su:
#!/bin/bash
su -c "psql -d database_name -c \"SELECT c_defaults FROM user_info WHERE c_uid = 'testuser'\"" postgres
And also sudo:
#!/bin/bash
sudo -u postgres -H -- psql -d database_name -c "SELECT c_defaults FROM user_info WHERE c_uid = 'testuser'"
You can connect to psql as below and write your sql queries like you do in a regular postgres function within the block. There, bash variables can be used. However, the script should be strictly sql, even for comments you need to use -- instead of #:
#!/bin/bash
psql postgresql://<user>:<password>@<host>/<db> << EOF
<your sql queries go here>
EOF