How to change H2 Database password using command Line - database

I would like to know whether there is a way we can change the H2 database password without login to the H2 Console.

If you lost your password and and database was encrypted, you can't read it without password in any way.
If database wasn't encrypted, you can use the recovery tool to export its content into SQL script.
java -cp h2-*.jar org.h2.tools.Recover -dir PATH_TO_DIRECTORY -db DATABASE_NAME
After that can create a new database and use that script to import data by execution of SQL command it in
RUNSCRIPT FROM '/PATH_TO_DIRECTORY/DATABASE_NAME.h2.sql'
or with RunScript tool
java -cp h2-*.jar org.h2.tools.RunScript -url NEW_URL -user NEW_USER -password NEW_PASSWORD -script PATH_TO_SCRIPT
If you have a password but want to change it, you need to execute ALTER USER userName SET PASSWORD 'newPassword'. You can do it from your application, or you can use a command-line shell tool:
java -cp h2-*.jar org.h2.tools.Shell

Related

Shell scripts who call sql scripts - convert isql istructions to sqlcmd istructions

we are migrating some shell scripts from unix to linux and we are bsolutely newby.
Some scripts invoke slq scripts which contain instructions for operations on a syabase database that we are migrating to sql server database, so we are rewriting these sql scripts as well.
Our new shell scripts call directly the sqlcmd command and already pass db server, db name, user and password stored in environment variables:
sqlcmd -S $SERVER_DB -d $NAME_DB -U $USER_DB -P $PASSW_DB < /home/scripts/update_data.sql
but at the first line of each sql script we need to convert, we are finding similar statement:
isql -U$DBUSER -P$DBPASSWD -w 80<< EOF|grep -v "return status" >>/usr/local/abc/ABC.txt
Perhaps because for some users a certain connection to the syabase database was set by default, so it was sufficient to put the call to the isql command on the first line of the sql file passing only the user and password, but in our case using sqlcmd we already pass everything directly in the shell script, it is not necessary to invoke the sqlcmd command again at the first line of the sql script.
So what should we write in place of isql call but leaving the part that redirects the output to the ABC.txt file?
Thanks in advance

Import/Export PostgreSQL db "without" pg_dump or sql file / backup, etc...?

I need to import a old db into a new postgre server.
Is there a way to migrate an old database to a new server without using pg_dump?
I don't have the sql file, or the old server backup file, neither the user and the password, just the physical files in the "\data" folder, is there any way to do this?
The target server is in the same version of th old server.
Thanks.
Well as a test you could try:
pg_ctl start -D $DATA
Where pg_ctl comes from the target version and the $DATA is the the /data directory. You have not said how you came to have just a /data directory. If this came from an unclean shutdown or a corrupted drive the possibility exists that the server will not start.
UPDATE
To get around auth failure find pg_hba.conf and create or modify local connection to use trust method. For more info see pg_hba and trust. Then you should be able to connect like:
psql -d some_db -U postgres
Once in you can use ALTER ROLE to change password:
ALTER ROLE <role_name> WITH PASSWORD 'new_password';

A handy .BAT file to update a local Sybase SQL DB

I need to make a change in my local Sybase DB quite often,
changing "version2" value to "10.076" and sometimes to "10.080".
If instead of opening the Interactive SQL tool, typing in DB's credentials each time, I could just run a "76.bat" or "80.bat" file, it would be very handy.
The query is:
UPDATE "trogxxx"."xxversion" SET "version2"=10.076 WHERE "version"='2002'
And credentials:
UserID: Trogxxx
Password: Trogxxx2018
ServerName: dem8
How would a .BAT file look like, in order to log in and run the update?
You could create a basic bat file calling to isql on the command line and use the -i flag to indicate the SQL script containing the update statement which then gets called as an input file. You could prompt for the -U (username) and -P (password) options or hardcode as much as you want.
Syntax: isql -U username -P password -S server -i input_filename
This assumes your environment has sufficient settings for your normal Sybase client e.g. %SYBASE% etc.

Executing set of SQL queries using batch file?

I am using a SQL Server database. I have these SQL queries:
Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;
Is it possible to run these scripts using a batch file? The database is a remote database.
Thanks!
Save the commands in a .SQL file, ex: ClearTables.sql, say in your C:\temp folder.
Contents of C:\Temp\ClearTables.sql
Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;
Then use sqlcmd to execute it as follows. Since you said the database is remote, use the following syntax (after updating for your server and database instance name).
sqlcmd -S <ComputerName>\<InstanceName> -i C:\Temp\ClearTables.sql
For example, if your remote computer name is SQLSVRBOSTON1 and Database instance name is MyDB1, then the command would be.
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
Also note that -E specifies default authentication. If you have a user name and password to connect, use -U and -P switches.
You will execute all this by opening a CMD command window.
Using a Batch File.
If you want to save it in a batch file and double-click to run it, do it as follows.
Create, and save the ClearTables.bat like so.
echo off
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
set /p delExit=Press the ENTER key to exit...:
Then double-click it to run it. It will execute the commands and wait until you press a key to exit, so you can see the command output.
Check out SQLCMD command line tool that comes with SQL Server. http://technet.microsoft.com/en-us/library/ms162773.aspx
Use the SQLCMD utility.
http://technet.microsoft.com/en-us/library/ms162773.aspx
There is a connect statement that allows you to swing from database server A to server B in the same batch.
:Connect server_name[\instance_name] [-l timeout] [-U user_name [-P password]]
Connects to an instance of SQL Server. Also closes the current connection.
On the other hand, if you are familiar with PowerShell, you can programmatic do the same.
http://technet.microsoft.com/en-us/library/cc281954(v=sql.105).aspx
Different ways:
Using SQL Server Agent (If local instance)
schedule a job in sql server agent with a new step having type as "T-SQL" then run the job.
Using SQLCMD
To use SQLCMD refer http://technet.microsoft.com/en-us/library/ms162773.aspx
Using SQLPS
To use SQLPS refer http://technet.microsoft.com/en-us/library/cc280450.aspx

Create Postgres database using batch file with [template],[encoding],[owner] and a .sql file

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. However, I want to create a database with the following parameters, as follows:
WITH OWNER = Myadmin
TEMPLATE = template0
ENCODING = 'SQL_ASCII'
TABLESPACE = pg_default
LC_COLLATE = 'C'
LC_CTYPE = 'C'
CONNECTION LIMIT = -1;
Please tell me how to create a database with the above parameters using Batch files.
Also let me know how to use a .sql file to do the same, like this command-line:
"C:\Program Files\PostgreSQL\9.0\bin\createdb.exe" -U Myadmin -f C:\createDB.sql;
The client program createdb does not support all those options.
Create a file db_create.sql:
CREATE DATABASE MydatAbseName
WITH OWNER myadmin
TEMPLATE template0
ENCODING 'SQL_ASCII'
TABLESPACE pg_default
LC_COLLATE 'C'
LC_CTYPE 'C'
CONNECTION LIMIT -1;
Call it:
psql -U postgres postgres -f C:/path/to/db_create.sql
The trick here is to connect to the default maintenance db "postgres" and create the new database from there. I do it with the default superuser named "postgres" in my example.
psql -f executes the SQL commands in the given file.
You could also just execute a single command with psql -c (no file involved):
psql -U postgres postgres -c "CREATE DATABASE MydatAbseName WITH OWNER Myadmin
EMPLATE template ENCODING 'SQL_ASCII' TABLESPACE pg_default LC_COLLATE 'C'
LC_CTYPE C' CONNECTION LIMIT -1"
More on creating a database in the fine manual here and here.
More on psql.
On Windows, it looks something like this:
"C:\Program Files\PostgreSQL\verson_number\bin\psql.exe" -U user -f C:/path/to/db_create.sql postgres
The last "postgres" is the name of the default maintenance db.
If you want to use it in a batch file you have to answer a password prompt or connect with a user that is allowed access without providing a password. Basics in chapters The Password File and The pg_hba.conf File of the manual. More here:
Run batch file with psql command without password

Resources