How to add Postgres extensions via .sh file - postgresql

I'm trying to add extensions for Postgres by writing the following script (setup.sh):
sudo -u postgres psql my_database
CREATE EXTENSION adminpack;
when I do vagrant up, it supposed to run and add extensions automatically by running the script. However, I got the error message that
==> default: ERROR: syntax error at or near "exit"
==> default: LINE 1: exit
==> default: ^
==> default: /tmp/vagrant-shell: line 24: CREATE: command not found
Please note that I have installed all the necessary postgres stuff to run the code above. In addition, when I enter these command manually, it successfully creates the extension. Thanks to whoever that helps.

try:
sudo -u postgres psql my_database -c "CREATE EXTENSION adminpack"
https://www.postgresql.org/docs/current/static/app-psql.html
-c command
--command=command
Specifies that psql is to execute the given command string, command.
also consider using -f sql_file.sql for more complicated scripts, or smth like:
psql <<EOF
\x
SELECT NOW();
SELECT * FROM foo;
EOF

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"

How to run the 'postgres' command (single-user)

I have postgres installed on an ubuntu machine, and I am able to enter into the command line via something along the lines of:
$ sudo -u postgres psql
psql (10.15 (Ubuntu 10.15-0ubuntu0.18.04.1))
Type "help" for help.
postgres=#
And I can start/stop the server by doing something like:
$ sudo service postgresql
Usage: /etc/init.d/postgresql {start|stop|restart|reload|force-reload|status} [version ..]
Those both seem fine. However, I would like to run postgres in single-user mode to do a couple tests. On the postgres page it gives a few examples, such as:
To start a single-user mode server, use a command like
postgres --single -D /usr/local/pgsql/data other-options my_database
However, if I use the 'postgres' command, I just get an error saying I don't have that command:
$ postgres
Command 'postgres' not found, did you mean:
What do I need to install to run the 'postgres' command in order to enter single-user mode?
as you have not export the binary path that's why it's can't find your binary of postgres.
use this command:
/usr/lib/postgresql/10/bin/postgres --single -D /usr/local/pgsql/data other-options my_database
or,
you can export the path in bash
first open the bashrc with this command:nano ~/.bashrc
add this line in the end :PATH="/usr/lib/postgresql/10/bin/:$PATH"
run this command source ~/.bashrc
the just use postgres --single -D /usr/local/pgsql/data other-options my_database
you can also find where your binary is with this command : find /usr/lib -iname 'postgres'
It is already installed, it is just not in your PATH, as it is not anticipated you would use it manually.
It is probably somewhere like "/usr/lib/postgresql/10/bin/postgres", or you can use locate or find to find it.
Ubuntu has conf files spread over several places so:
/usr/lib/postgresql/13/bin/postgres --single -D /var/lib/postgresql/13/main -c "config_file=/etc/postgresql/13/main/postgresql.conf"

PostgreSQL pg_dump syntax error in Ubuntu

In ubuntu 16.04 when i want to run pg_dump it doesn't work i got this error syntax error. What is wrong ?
postgres=# pg_dump db_name > db_name1.sql
postgres-#
postgres-# ;
ERROR: syntax error at or near "pg_dump"
LINE 1: pg_dump db_name > db_name1.sql
^
postgres=#
pg_dump is not an SQL command.
It is a stand-alone utility, so you cannot run it from SQL query.
Follow the link for more information.
For your case: type \q<Enter> to quit from SQL-client and repeat your command again using shell prompt.
Remember you should use your SQL-credentials.
I.e.:
$ pg_dump -U <postgres_user_name> db_name > db_name1.sql`

altering privilege in psql interactive prompt

I am new to the use of psql prompt. I ran sudo -u postgres (psql version 9.3.9)
Then I connected to my database and I ran
=> ALTER DEFAULT PRIVILEGES FOR USER some_user IN SCHEMA public GRANT SELECT ON TABLES TO other_user;
and then I got this error message
ERROR: syntax error at or near "ALTER DEFAULT PRIVILEGES FOR USER"
LINE 1: ALTER DEFAULT PRIVILEGES FOR USER ckan_default IN SCHEMA pub..
How can I deal with that ?

Unable to restore the postgresql data through command prompt

I am trying to restore the postgres sql data from a file . I am trying to do so but it is not importing .
Here is the command which i am using:
postgres-# psql -hlocalhost -p5432 -u postgres -d test -f C:/wamp/www/test/database_backups/backup004.sql
Please help me what I am doing wrong .
I am using windows and the above command does not throws any error but it does not import data.
Regards
Surjan
The only immediate thing I can see there is the capitilsation of -u for username (should be -U).
Correction: You're typing the command line into the psql shell.
You should exit to the CMD.EXE shell, and try the command there. With the correct capitalisation of -U, by the way.
OR, use this to replay the script into that psql shell:
\i C:/wamp/www/test/database_backups/backup004.sql
The forward slashes don't cause a problem on my Windows machine.