Can a connection command be embedded in a psql script? - postgresql

I am running a script interactively to test it. The meta-command \c is not playing well with the standard sql commands: it seems to want to be run by itself. Here is what happens when it is run in the script
\set username steve
\c pubkey :username
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO :username;
GRANT ALL PRIVILEGES ON ALL sequences IN SCHEMA public TO :username;
Results:
invalid integer value "ALL" for connection option "port"
Previous connection kept
But if the "\c" command is run by itself then the subsequent grant's are successful. Is there a way to embed the "\c" connection commands in a psql script?

It seems that running in psql has different behavior than in a script itself: running the above from the command line using -f does work
psql postgres -f create_dbs.sql -a > db.out 2>&1
Whereas running the '\c` command from psql seems to need to be isolated.

Related

psql faild to restor file from network drive

i have a dump file on drive z (network drive)
im opening the psql from PgAdmin4
this is the command that im writeing:
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
and this is the error that im getting:
Invalid command \DB_BU. Try \? for help.
when im doing this:
psql -U postgres -d postgres -f i\ Z:\DB_BU\md_20220729.sql
Invalid command \DB_BU. Try ? for help.
and when im doing this:
psql -U postgres -d postgres -f "Z:\DB_BU\md_20220729.sql"
im not getting any error but also its not restoring the file. how can i restor the file?
You're trying to call psql from within psql or PGAdmin. Since
psql is a standalone program, not an SQL command you can run in PGAdmin SQL window or psql's own, internal meta-command you're getting the error
Invalid command \DB_BU. Try \? for help
indicating that there was an attempt to interpret your entire command as a SQL query or an internal command and that this attempt failed.
You can open "psql tool" from within PGAdmin but your command won't work there either because it's trying to call psql itself, with some command-line options, which you cannot do when you're already inside an interactive psql session. The command
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
can be used outside psql and PGAdmin, in your terminal, like zsh on Mac, sh/bash on Linux, cmd or PowerShell on Windows, where psql is installed and visible, along with your network path.
If you're able to open the psql tool window in PGAdmin, you can instead try and use an internal psql \i meta-command which is basically the same thing as the -f command-line option, but meant for use inside the psql session:
\i "Z:\DB_BU\md_20220729.sql"

PostgreSql Equivalent of sqlplus -S username/password \#lock?

What will be Postgres equivalent of following:
sqlplus -S username/password \#lock.
Also what does #lock means here?
I don't know PostgreSQL, but - as of Oracle, here you are:
That command means that
you'll connect to Oracle's command line tool called SQL*Plus (executable name is sqlplus)
in silent mode (that's what -s does)
providing username and password
and execute .SQL script whose name is lock (I have no idea what it does; you'll have to open it in any text editor and have a look)
Now, how someone establishes connection to PostgreSQL and runs a .SQL script, that's something I wouldn't know, but - reading online documentation - it might be
psql -U username -d database_name -a -f lock
According to the explanations in the comments and the other answer, the equivalent in PostgreSQL should be
psql 'user=username password=password dbname=mydatabase' -f lock

psql script execution not returning error

I am trying to execute multiple sql scripts using psql. I created one master script with all the scripts to be executed as below. I want all the scripts to succeed or fail together.
master.sql
BEGIN;
\i one.sql
\i two.sql
\i three.sql
COMMIT;
I am trying to catch the error code of psql to determine if it success. When i query for ?$ it always returns 0. I figured when add ON_ERROR_STOP=1 to command it returns proper error code. Problem with this approach if the error happens in three.sql it does not roll back one.sql and two.sql transactions.
psql -U postgres -h localhost -d test -v ON_ERROR_STOP=1 -f master.sql
What would be correct approach to find out if script executed successfully.

Creating role and database PostgreSQL not working

First I run the command:
sudo su _postgres
Then I run the command:
create role mixeddrinks with createdb login password 'password1'
But it comes back with:
-bash: create: command not found
I’m not very familiar with the Terminal and with PostgreSQL so I’m not sure what I am doing wrong I am trying to create a role and a database.
First I run the command sudo su _postgres, then I run the command create role mixeddrinks with createdb login password 'password1'
You're mixing up shell commands and the psql command line.
If you want to use SQL, you need to use the psql command. sudo su _postgres is an inefficient way of getting a unix command shell as the _postgres user. It doesn't give you an SQL shell. You can run unix commands like psql, createuser, etc from the unix command shell. You can tell you're at the command shell because the prompt looks something like:
postgres$
If you want an SQL shell, so you can run commands like CREATE USER, etc, you need to run psql. If you want a superuser SQL shell, that'd be something like:
sudo -u _postgres psql
This will give you a prompt like:
postgres=#
where you can run SQL commands. Remember that SQL commands end with a semicolon.
postgres=# create role mixeddrinks with createdb login password 'password1';

drop db in postgres

I try dropdb mydbname in shell. It do not give any error. But still when I call \l it is still there.
I logged into the postgres server using sudo -u postgres psql.
Other than my main concern I need to know how to go into the database other than just staying outside of it. (as a example if I want to list the tables)
In order to drop database you can use SQL command (but I do not understand why dropdb didn't work) DROP DATABASE mydbname:
sudo -u postgres psql -c "DROP DATABASE mydbname"
It would be good to check if database is not used:
select * from pg_stat_activity where datname = 'mydbname';
The sudo -u postgres psql connects to postgres database. You need to specify database: sudo -u postgres psql mydbname and then you can use metdata commands like \d, \dt, \dv, ...
When you say "shell" ... do you mean the psql shell, not the unix command line shell?
I'd say you're running into this issue:
Postgresql not creating db with “createdb” as superuser, yet not outputting errors
ie you're trying to use dropdb as a psql command, when it's a unix shell command. You're not getting an error because of this:
In psql, why do some commands have no effect?
You didn't terminate the command with a semicolon.
Are you missing the comma(;)? This command worked for me:
drop database <database_name>;
Server should be running, then:
dropdb <database name>
If server is not running, first try:
pg_ctl start -D <mylocal_db_path> -l <mylogfile.log>