PSQL database creation from script - scripting

I have an issue with automatic database creation. I have created create_database.sql file with following code:
BEGIN;
\i schema.sql
\i domains.sql
\i sequences.sql
\i tables.sql
\i dane/insert_users.sql
\i dane/insert_default_expenses.sql
\i dane/insert_default_incomes.sql
\i dane/insert_default_payments.sql
\i dane/insert_user_incomes.sql
\i dane/insert_user_expenses.sql
\i dane/insert_user_payments.sql
\i dane/insert_expenses.sql
\i dane/insert_incomes.sql
\i procedures.sql
\i views.sql
COMMIT;
I would like to automatically create database using that transaction. So I am typing inside SQL SHELL:
psql budget < 'C:\Users\xxx\Desktop\yyy\create_database.sql'
There is no error returned but also DB is not being updated. Any ideas why ?

For running a SQL file you need to provide the file by using the following command
./psql -U postgres -d postgres -p 5432 -f 'C:\Users\xxx\Desktop\yyy\create_database.sql'
Also in create_database.sql you need to provide full path for all sql files that you are executing from inside the tracnsaction block e.g
\i C:\Users\xxx\Desktop\yyy\schema.sql

Related

Dropped the default postgres database in Postgresql server

I did not know what the database did and I deleted it without realizing that now I wouldn't be able to run psql again. How do I get thngs back to normal again?
The postgres database isn't really needed.
But you can re-create it using:
psql -U postgres -d template1
psql (13.1)
postgres=# create database postgres;
The -d template1 tells psql to connect to the template1 database.

Cannot make granting access to existing tables work

I'm failing at adding a new DB role, that would have a SELECT privilege on tables from particular database.
My problem is that the role is not able to SELECT from a table in existing DB.
Here's my failing test case (written so it can safely be copy-pasted into a /tmp/test.sh and executed):
# --- cleanup objects, if any
psql -U postgres -c "REVOKE SELECT ON ALL SEQUENCES IN SCHEMA public FROM db_reader"
psql -U postgres -c "REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM db_reader"
psql -U postgres -c "REVOKE USAGE ON SCHEMA public FROM db_reader"
psql -U postgres -c "DROP ROLE IF EXISTS db_reader"
psql -U postgres -c "DROP DATABASE IF EXISTS some_existing_db"
# --- test
psql -U postgres -c "CREATE DATABASE some_existing_db"
psql -U postgres some_existing_db -c "CREATE TABLE cats (name varchar(10))"
psql -U postgres some_existing_db -c "INSERT INTO cats (name) VALUES ('a'), ('b')"
psql -U postgres -c "CREATE ROLE db_reader WITH login"
psql -U postgres -c 'GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO db_reader'
psql -U postgres -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO db_reader'
psql -U postgres -c 'GRANT USAGE ON SCHEMA public TO db_reader'
psql -U db_reader some_existing_db -c "SELECT COUNT(1) FROM cats"
Looks like I'm missing something extremely, embarrassingly obvious here, as the above fails with the following error:
ERROR: permission denied for relation cats
Why?
You are missing that databases are logically separated.
Your GRANT statements are executed in database postgres (if you do not specify a database name, psql will try to connect to a database with the same name as the database user).
Consequently, the effect of these grants is limited to the database to which you are connected.
You have to add some_existing_db to the psql invocations where you grant privileges to db_reader.

How to run alter table script in postgres using bash

I wanted to run the alter table command using bash script. I managed to create the table, load the basemodel, create config tables and etc. The script will login to the postgres database before it is execute the alter table command. It stuck as (abcdb=> ) without proceed to the alter table command. Is there any way to make sure the alter table able to execute?
The login as
psql -h 191.169.51.10 -d abcdb -U myname
alter table attr_config rename regexp to regexp_val;
alter table class_action_config rename type to type_name;
alter table funcitem_config rename type to type_name;
In order to run a script like this you need to redirect the SQL/DML (alter table statements) into the psql command. Otherwise bash won't understand what to do with them.
psql -h 191.169.51.10 -d abcdb -U myname << EOF
alter table attr_config rename regexp to regexp_val;
alter table class_action_config rename type to type_name;
alter table funcitem_config rename type to type_name;
EOF
Alternatively you can put your SQL/DML into a separate file and have psql to read from that:
psql -h 191.169.51.10 -d abcdb -U myname < alter_statements.sql
Or
psql -h 191.169.51.10 -d abcdb -U myname -f alter_statements.sql

How do you connect to a postgresql db from a file NOT the command line? [duplicate]

In MySQL, I used use database_name;
What's the psql equivalent?
In PostgreSQL, you can use the \connect meta-command of the client tool psql:
\connect DBNAME
or in short:
\c DBNAME
You can connect to a database with \c <database> or \connect <database>.
At the PSQL prompt, you can do:
\connect (or \c) dbname
You can select the database when connecting with psql. This is handy when using it from a script:
sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test
use \c databaseName or \connect databaseName
(Working on psql 13.3)
\l for databases
\c DatabaseName to switch to db
\df for procedures stored in particular database
Though not explicitly stated in the question, the purpose is to connect to a specific schema/database.
Another option is to directly connect to the schema. Example:
sudo -u postgres psql -d my_database_name
Source from man psql:
-d dbname
--dbname=dbname
Specifies the name of the database to connect to. This is equivalent to specifying dbname as the first non-option argument on the command line.
If this parameter contains an = sign or starts with a valid URI prefix (postgresql:// or postgres://), it is treated as a conninfo string. See Section 31.1.1, “Connection Strings”, in the
documentation for more information.
Using psql's meta-command \c or \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo (see documentation).
Example: \c MyDatabase
Note that the \c and \connect meta-commands are case-sensitive.
Use below statement to switch to different databases residing inside
your postgreSQL RDMS
\c databaseName
You can also connect to a database with a different ROLE as follows.
\connect DBNAME ROLENAME;
or
\c DBNAME ROLENAME;
You can connect using
\c dbname
If you would like to see all possible commands for POSTGRESQL or SQL follow this steps :
rails dbconsole
(You will be redirected to your current ENV database)
?
(For POSTGRESQL commands)
or
\h
(For SQL commands)
Press Q to Exit
If you want to switch to a specific database on startup, try
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql vigneshdb;
By default, Postgres runs on the port 5432. If it runs on another, make sure to pass the port in the command line.
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p2345 vigneshdb;
By a simple alias, we can make it handy.
Create an alias in your .bashrc or .bash_profile
function psql()
{
db=vigneshdb
if [ "$1" != ""]; then
db=$1
fi
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p5432 $1
}
Run psql in command line, it will switch to default database; psql anotherdb, it will switch to the db with the name in argument, on startup.
Listing and Switching Databases in PostgreSQL
When you need to change between databases, you’ll use the \connect command, or \c followed by the database name as shown below:
postgres=# \connect database_name
postgres=# \c database_name
Check the database you are currently connected to.
SELECT current_database();
PostgreSQL List Databases
postgres=# \l
postgres=# \list
Connect to database:
Method 1 : enter to db : sudo -u postgres psql
Connect to db : \c dbname
Method 2 : directly connect to db : sudo -u postgres psql -d my_database_name
You can just enter use [dbName] to switch between databases without reentering your password.

How to pass arguments to .sql file from shell script

Hi there if anyone can help me, I have a .sh script that executes 4 .sql scripts, each executing against a schema. Currently the schema name is hardcoded but i want to make it configurable.
Given the following below how will i pass the arguments from the shell script to the .files?
an e.g call to a .sql is done in my shell script is done so like the following
ECHO “DELETING SCHEME….”
psql -f $SCRIPT_DIR/delete_data.sql my_db postgres
ECHO “DATABASE SCHEMA DELETED..”
delete_data.sql
drop schema my_schema cascade;
create schema my_schema;
You could replace the my_schema part with a placeholder, like %SCHEMA%:
drop schema %SCHEMA% cascade;
create schema %SCHEMA%;
We then run a substitution using sed, and pipe the results into psql (reading from stdin is equivalent to reading from file):
sed "s/%SCHEMA%/$schemaName/" $SCRIPT_DIR/delete_data.sql | psql powa_aim_db postgres
You can do this using a heredoc for your SQL:
my_schema="$1"
ECHO “DELETING SCHEME….”
psql <<SQL
drop schema $my_schema cascade
create schema $my_schema
SQL
ECHO “DATABASE SCHEMA DELETED..”
Then call your script with the schema name as the first argument:
$ ./my_script my_schema_name