executing a .sql file through ubuntu command line for postgres - postgresql

I have installed postgresql on ubuntu using:
$ sudo apt install postgresql
Now, I have a series of sql queries I would like to fire to create schemas and users and tables etc. I have put those queries in a .sql file as below:
$ sudo nano postgressetup.sql
CREATE SCHEMA schma;
CREATE USER a2i WITH PASSWORD 'password';
GRANT CONNECT ON DATABASE postgres TO schma;
This file has all the queries. I tried something like:
$ psql -U postgres -d postgres -a -f postgressetup.sql
and received error:
psql: FATAL: Peer authentication failed for user "postgres"
I want to know the way I can execute this .sql file.
Note: I've just installed postgres and no further operation is done on it. Any help is appreciated.

You can use the following command explicitly providing db context user
sudo -u postgres psql -U postgres -d postgres -a -f postgressetup.sql

Related

Modifying postgresql.conf through shell?

I want to modify a postgresql.conf parameter through the shell. From the documentation I can see that I can use the postgres command with the -c flag.
However, on my attempt, for example,
postgres -c autovacuum=off
postgres returns:
Execution of PostgreSQL by a user with administrative permissions is not permitted.
The server must be started under an unprivileged user ID to prevent possible system security compromises. See the documentation for more information on how to properly start the server.
How can I overcome this or what is the correct procedure? Also, I don't really mind for security compromises.
Given the differences on the underlying OS, I usually prefer to do this via PostgreSQL itself, which comes handy when you're dealing with a managed service that do not give you filesystem access, like so:
sudo -u postgres psql -U postgres -d database_name -c "alter system set postgresql_parameter = 'new_value';"
As an example when I have to install TimeScaleDB extension, I can do:
sudo -u postgres psql -U postgres -d database_name -c "alter system set shared_preload_libraries = 'timescaledb';"
sudo service postgresql restart
sudo -u postgres psql -U postgres -d database_name -c "create extension if not exists timescaledb;"

Create database from command line

I am trying to create a database from command line.
My OS is centos and postgres version is 10.9.
sudo -u postgres psql createdb test
Password for user test:
Why is it prompting me for the password?
Change the user to postgres :
su - postgres
Create User for Postgres (in the shell and NOT with psql)
$ createuser testuser
Create Database (same)
$ createdb testdb
Acces the postgres Shell
psql ( enter the password for postgressql)
Provide the privileges to the postgres user
$ alter user testuser with encrypted password 'qwerty';
$ grant all privileges on database testdb to testuser;
Try:
sudo -u postgres psql -c 'create database test;'
createdb is a command line utility which you can run from bash and not from psql. To create a database from psql, use the create database statement like so:
create database [databasename];
Note: be sure to always end your SQL statements with ;
As some of the answers point out, createdb is a command line utility that could be used to create database.
Assuming you have a user named dbuser, the following command could be used to create a database and provide access to dbuser:
createdb -h localhost -p 5432 -U dbuser testdb
Replace localhost with your correct DB host name, 5432 with correct DB port, and testdb with the database name you want to create.
Now psql could be used to connect to this newly created database:
psql -h localhost -p 5432 -U dbuser -d testdb
Tested with createdb and psql versions 9.4.15.
As the default configuration of Postgres, a user called postgres is made and the user postgres has full super admin access to entire PostgreSQL instance running on your OS.
sudo -u postgres psql
The above command gets you the psql command line interface in admin mode.
Creating user
sudo -u postgres createuser <username>
Creating Database
sudo -u postgres createdb <dbname>
NOTE: < > are not to be used while writing command, they are used just to signify the variables
PostgreSQL Create Database - Steps to create database in Postgres.
Login to server using postgres user.su - postgres
Connect to postgresql database.
bash-4.1$ psql
psql (12.1)
Type "help" for help.
postgres=#
Execute below command to create database.
CREATE DATABASE database_name;
Check for detailed information below:
https://orahow.com/postgresql-create-database/
PGPORT=5432
PGHOST="my.database.domain.com"
PGUSER="postgres"
PGDB="mydb"
createdb -h $PGHOST -p $PGPORT -U $PGUSER $PGDB
With a single command line:
su -c "createuser dbuser;createdb -h localhost -p 5432 -E UTF8 -O dbuser dbname;" - postgres
It is pretty simple but sometimes I find the answers tricky.
[For windows users]
Open Windows cmd
psql -U <username>
Once connected to psql, enter the following command to create a new database: CREATE DATABASE <database_name>;
To verify that the database has been created, you can run the \l command to list all available databases. Your new database should be listed in the output.------[Additional]------
You can now connect to the new database using the \c command followed by the database name, like this: \c <database_name>
You can now run SQL commands on the new database to create tables, insert data, and so on.
Note: Make sure to replace <username>, <database_name> with your actual Postgres username and database name.
If you are using pgAdmin:
In query editor you can try like this :
CREATE DATABASE <databasename>
WITH
OWNER = <dbowner>
ENCODING = <encoding>
CONNECTION LIMIT = <numberofsimulaneousconnections>;
an example snippet :
CREATE DATABASE twitterdb
WITH
OWNER = postgres
ENCODING = 'UTF8'
CONNECTION LIMIT = -1;

Setting up postgres on VPS for production

So, I have a Pyramid app with a postgres database on my local machine. I did a pg_dump to get a dump of the data in my database, called pg_dump_2014-04-22. I then git pushed this file, and did a git pull in the VPS to get the file.
Now, I have already installed postgres on my VPS. When I sudo -u postgres psql on my VPS, I can connect to it but there are no relations (naturally).
Both my username and database name are postgres.
So, I tried psql postgres < pg_dump_2014-04-22, but this gives the error psql: FATAL: role "root" does not exist.
I also tried pg_restore -h localhost -U postgres -d postgres pg_dump_2014-04-22, and that prompts me for my password, but then throws the error pg_restore: [archiver(db)] connection to database "postgres" falied: FATAL: password authentication failed for user postgres"
What am I missing here?
You first have to create a user and the database where you want to import your dump
su postgres
createuser root
createdb yourdb
Then import the dump with
psql -d yourdb -f pg_dump_2014-04-22

How to run postgresql command from GCE shell?

Installing PostgreSQL on GCE requires root password to run sudo -u postgresql. This prompts for a password, which I was never given.
How do I get this pass, or any way to run postgresql commands from the shell in a different way?
Your system user postgresql doesn't have a password (I state with no proof... but I think you'll find this to be true.)
Normally you should use commands like these:
# Test that YOU can use psql (as postgres) to run a query:
psql -U postgres -c 'select * from pg_catalog.pg_user;'
# Test an interactive session:
psql -U postgres my_database
my_database=# select 42 as the_answer;
# Create a new database
psql -U postgres my_database
my_database=# create database mydb;
Alternatively, it's probably possible to login like this (it usually is):
sudo su postgres
And you could probably use this to run createdb. But running psql as yourself is probably better.
If you want to run commands against your postgresql server you should not need to use sudo, just use this syntax to enter the Postgresql Interactive Shell:
psql -U username database_name
OR
psql -U username hostname database_name
Replacing username with your postgresql usename, hostname (if not running on the same server) with the servers host name and database_name with the name of your database. For example:
psql -U postgressql customers
Normally sudo requires your user account password. So assuming that the account you are running the command from is listed in the sudoers file, the password it is prompting you for should be your own. Have you tried that, as opposed to the root or the postgresql password, which you don't appear to have (or might not even be set).

Fail to restore a postgres db

i had no problem to restore a postgres database but today when i tried to restore it as before it gave me a error, here whats i have done:
i tried to restore a postgres database as follows:
mostafa#jamareh:~/.../DB$ sudo -u postgres psql -d dbname -f dbname.sql
but it gives me this:
could not change directory to "/home/....../DB"
dbname.sql: No such file or directory
also i tried sudo -su postgres... but it doesn't work neither
what can i do to resolve it ?
thanks in advance,
From an initial look, it could be that the postgres UNIX user does not have access to your dbname.sql file because of permission issues.
You could either change the permissions (or move the file to a place that is readable by UNIX user postgres, say, /tmp), or, better, restore the database from your own UNIX user, but with the proper database user. For example, you would have:
mostafa#jamareh:~/.../DB$ psql -d dbname -U postgres -f dbname.sql
Note that there is no sudo, but that you tell psql to run the script as DB user postgres (assuming that you have the credentials for the postgres DB user).