how to run db2 sql command from a command line? - command-line

how can I run
sql command UPDATE CONTACT
SET EMAIL_ADDRESS = 'mytestaccount#gmail.com'
via command line
for db2 database
on linux
from a shell script file?

You need to be logged into your linux machine with an id that has "db2profile" in it's .profile.
If you are not sure what I'm talking about, look at the .profile of the db2 instance owner (usually db2inst1 unless you changed it during the install). The simplest thing would probably be to log in as the db2 instance owner.
Once you are logged in, type "db2" at the command line. If "db2" is not found, then recheck the .profile stuff.
To execute your sql command type the following at the command line (substitute with the name of the database you want to connect to):
db2 connect to <database name>
db2 UPDATE CONTACT SET EMAIL_ADDRESS = 'mytestaccount#gmail.com'

Related

SQL Shell (psql) don't run any commands on Windows [duplicate]

I'm new to Postgres and I set up a database and table. On the Ubuntu 18.04 command line (on a cloud server) I issued the following command using psql:
INSERT INTO psq01(date, email, firstname, lastname, password, comments)
VALUES ("052419", "mjs#outlook.com", "John", "Smith", "blank", "No comment")
After I issue that command, the psql command prompt switches from =# to -#, which means the transaction is not finished, so I issued COMMIT, but the prompt still shows as -#.
My question is: what do I do after an insert into command? Why am I getting the -# prompt?
The dbname-# prompt means that psql is waiting for a continuation line because you haven't terminated the statement with a semiculon (;) yet.
Either type a single semicolon and Enter to finish the command or type Ctrl+C to interrupt it and start over.
Are you sure that you want to use a superuser for inserting into a table?

How can I automate running an init script when invoking a postgres DB using psql?

psql (9.6.7, server 9.5.2) on linux
I have an init script... ~/sql.ini that I always want to run after connecting to a PG DB. I can do this by typing "\i ~/sql.ini", but is there a way to do this on the command line (which I alias) ?
Just put your commands in ~/.psqlrc file.
Your alias should call
PSQLRC=~/sql.ini psql
That environment variable specifies the startup file that psql executes automatically.

using pg_dump on remote desktop

I am a novice postgres user employing pgAdminIII on a Windows desktop to connect to a remote postgres db. It connects ok, and everything from within the gui works fine on a very small database. Now I need to make a dump of the whole database (for example called 'mydb') onto my local desktop. I open the command line tool plugin psql.exe and see the prompt
mydb=>
I write this:
mydb=> pg_dump mydb > /users/username/desktop
on pressing Enter, the screen returns
mydb->
( => has become ->) and there it stays for as long as I leave it. No file is written.
I cannot find in documentation the significance of => and -> and would be grateful for assistance.
pg_dump is an executable that is run from the o/s command line, not from within psql.
First: pg_dump is not a SQL statement. It's a program that you run like psql.exe
So to run that locally you need:
pg_dump mydb > c:\users\username\desktop
pg_dump accepts the same connection parameters as psql
The different types of prompts are explained in the manual - although that is somewhat hidden:
https://www.postgresql.org/docs/current/static/app-psql.html#APP-PSQL-PROMPTING
You can enter \set to see the current definition of those three different prompts.

How can I start PostgreSQL on Windows?

I have installed Postgresql on my Windows 10 PC. I have used the pgAdmin II tool to create a database called company, and now I want to start the database server running. I cannot figure out how to do this.
I have run the start command on the postgres command line, and nothing seems to happen.
What I doing is:
postgres=# pg_ctl start
postgres=# pg_ctl status
postgres=# pg_ctl restart
postgres=# pg_ctl start company
postgres=# pg_ctl status
.....-> I am seeing nothing returned.
Go inside bin folder in C drive where Postgres is installed.
run following command in git bash or Command prompt:
pg_ctl.exe restart -D "<path upto data>"
Ex:
pg_ctl.exe restart -D "C:\Program Files\PostgreSQL\9.6\data"
Another way:
type "services.msc" in run popup(windows + R).
This will show all services running
Select Postgres service from list and click on start/stop/restart.
Thanks
pg_ctl is a command line (Windows) program not a SQL statement. You need to do that from a cmd.exe. Or use net start postgresql-9.5
If you have installed Postgres through the installer, you should start the Windows service instead of running pg_ctl manually, e.g. using:
net start postgresql-9.5
Note that the name of the service might be different in your installation. Another option is to start the service through the Windows control panel
I have used the pgAdmin II tool to create a database called company
Which means that Postgres is already running, so I don't understand why you think you need to do that again. Especially because the installer typically sets the service to start automatically when Windows is started.
The reason you are not seeing any result is that psql requires every SQL command to be terminated with ; in your case it's simply waiting for you to finish the statement.
See here for more details: In psql, why do some commands have no effect?
If you have installed postgres via the Windows installer you can start it in Services like so:
After a lot of search and tests i found the solution :
if you are in windows :
1 - first you must found the PG databases directory
execute the command as sql command in pgAdmin query tools
$ show data_directory;
result :
------------------------
- D:/PG_DATA/data -
------------------------
2 - go to the bin directory of postgres in my case it's located "c:/programms/postgresSql/bin"
and open a command prompt (CMD) and execute this command :
pg_ctl -D "D:\PSG_SQL\data" restart
This should do it.
The simplest way to start/stop/restart the installed PostgreSQL Server on your Windows device is as follows:
Start -> net start postgresql-x64-14
Stop -> net stop postgresql-x64-14
Restart -> net stop postgresql-x64-14 && net start postgresql-x64-14
The version number must be changed to take into account the installed version of your PostgreSQL Server.
For windows the following command worked well for me
pg_ctl.exe restart -D "<path_to_data>"
Eg: pg_ctl.exe restart -D "D:\Program Files\PostgreSQL\13\data"
If you are getting an error "psql.exe' is not recognized as an internal or external command,... "
There can be :
Causes
System is unable to find the psql.exe tool, because the path to this tool is not specified in the system environment variable PATH
or
- PostgreSQL Database client not installed on your PC
Since you have already installed PostgreSQL the latter can not be the issue(assuming everything is installed as expected)
In order to fix the first one "please specify the full path to the bin directory in the PostgreSQL installation folder, where this tool resides."
For example
Path: "C:\Program Files\PostgreSQL\10\bin"
I found using
net start postgres_service_name
the only reliable way to operate Postgres on Windows
first find your binaries file where it is saved.
get the path in terminal mine is
C:\Users\LENOVO\Documents\postgresql-9.5.21-1-windows-x64-binaries
(1)\pgsql\bin
then find your local user data path, it is in mostly
C:\usr\local\pgsql\data
now all we have to hit following command in the binary terminal path:
C:\Users\LENOVO\Documents\postgresql-9.5.21-1-windows-x64-binaries (1)\pgsql\bin>pg_ctl -D "C:\usr\local\pgsql\data" start
all done!
autovaccum launcher started! cheers!
Remove Postmaster file in "C:\Program Files\PostgreSQL\9.6\data"
and restart the PostgreSQL services
There are different way to open PostgreSql database .
1> One of them is by going windows and select pgAdmin4 or pgAdmin3 depends to version you use and entering password you can access you database .
2> Another one is by terminal :
To able to select from terminal you have to add the path of your installed postgresql by going enviroment variables . To do that got to installed postgresql file and select the path of bin and add to enviroment variable of window setting .
after that you can type in terminal : psql -U postgres -h localhost
Hit enter and it ask you password . After giving password you can create database and tables and can access it .
I was try to solve the problem with Windows Terminal and I've cannot to solve it. Use Windows R + cmd (if you are using Windows) for it work!
The easiest way to enable pg_ctl command is to go to your PostgreSQL directory ~\PostgreSQL\version\bin\ and execute the pg_ctl.exe. Afterwards the pg_ctl commands will be available.

How do I execute a query automatically in PostgreSQL when connecting via shell?

I want to be able to execute a statement automatically when I connect to Postgres with psql and then remain connected so I can type in further commands.
Currently, every time I connect, the first thing I do is type:
SET search_path = 'something';
Or maybe I would want to do something else like:
SELECT COUNT(*) FROM sometable;
I know there is a -c argument to psql that will execute a command and then exit. I'm looking for a way I can execute a command upon connecting and then remain in the client.
(Note: I prefer not to alter the database, schema or role to make the search_path permanent, as all of the solutions I have found seem to dictate. I want to set it every time I connect. But again, this question could apply any SQL statement.)
I have tried this:
echo "SET search_path TO 'mything'" | psql
but that behaves the same way as:
psql -c "SET search_path TO 'mything'"
Is what I'm asking for doable?
psql will look for, and execute any commands found in, a couple of places every time it starts up. One is the system-wide psqlrc file, and one is in the home-directory of the login that's running psql, ~/.psqlrc.
So, you could add the SET command that you always want to be run, to your .psqlrc file, and it'll get executed every time you start up. See the example below:
~ $ cat ~/.psqlrc
SET search_path='mything';
~ $ psql
SET search_path='mything';
SET
psql (8.4.20, server 9.2.10)
WARNING: psql version 8.4, server version 9.2.
Some psql features might not work.
Type "help" for help.
rhnschema=# show search_path;
search_path
-------------
mything
(1 row)
rhnschema=#