What is command to run postgresql in Ubuntu 20.04 [closed] - postgresql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
The command to run psql in ubuntu 20.04.
psql command is not working well. How to create database also in ubuntu terminal.

The First command you will fire from your ubuntu terminal
sudo -i -u postgres
Then you can fire command
psql to go into postgres console.
Then you can create database.
CREATE DATABASE mydb

Check service status and start, stop or restart:
sudo service postgresql command (status, start, stop, restart)
e.g.
sudo service postgresql status
sudo service postgresql start
Login:
Default: sudo -u postgres psql
Details: sudo -u username psql database name
e.g.
sudo -u postgres mydb
Create Database:
Create database database name;
e.g.
Create database mydb;
View all databases:
To see all databases
\l

Related

executing a .sql file through ubuntu command line for postgres

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

Cannot connect to the database in PostgreSQL

Terminal shows you are connected to database. While sending request from Postman it shows:
Cannot connect to database [default].
I have tried different solutions provided in Stack Overflow but it still doesn't work.
What I have done:
db{
107 default.driver=org.postgresql.Driver
108 default.url="jdbc:postgresql://localhost:5432/student"
109 default.user=postgres
110 default.password=root
And this is what I have done in Terminal is:
createdb -h localhost -p 5432
postgres student password root
When I get some kind of PostgreSQL connection error the
first step I take is to test if `psql' works by using the following command
$ psql -d mypgdatabase -U mypguser
If this doesn't give us a clue then the second step is to see if `pg_hba.conf' file has correct permission for our user, for example it should contain a like like this
local all all trust
After editing this file don't forget to reload postgresql service by issuing the following command
$ sudo /etc/init.d/postgresql reload
Now try again step 1.
This is all beautifully documented in Debian PostgreSQL wiki page here https://wiki.debian.org/PostgreSql
This works:
postgres=# sudo -u postgres psql postgres
postgres-# \password postgres
Enter new password:
Enter it again:
In my case password is root as I have keep my default password as root.

postgresql: no such file or directory

I'm trying to install a postgresql database on my VPS server by using this guide.
In there it says
sudo apt-get install postgresql postgresql-contrib libpq-dev
And then
sudo su - postgres
createuser --pwprompt
exit
When I run createuser --pwprompt it asks me to create a password, once I fill it in I get the following error:
createuser: could not connect to database postgres: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
If I do psql --version I get psql (PostgreSQL) 9.4beta3
And which psql outputs /usr/bin/psql
Any idea on how to fix this problem?
Update
Running pg_ctl gives another error
WARNING:root:could not open file '/etc/apt/sources.list.d/passenger.list'
The program 'pg_ctl' is currently not installed. To run 'pg_ctl' please ask your administrator to install the package 'postgres-xc'
Althgough reading up on postgres-xc shows quite a lot of other problems.

Postgres on Docker. How can I create a database and a user?

I'm trying to create simple postgres server with docker. I use the official postgres image as a base for my container.
My Dockerfile contains these commands:
FROM postgres
USER postgres
RUN /etc/init.d/postgresql start &&\
psql --command "CREATE USER user WITH SUPERUSER PASSWORD 'user';" &&\
createdb -O user app
And when I try to run it I have an error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
What I'm doing wrong?
It's possible that it takes some time for postgres to start accepting connections. The way you've written it, it will call CREATE USER immediately after the start function returns. Try putting a sleep in there and see if it's still a problem.
Use pg_ctl with the -w flag so the command will finish when the server has started. No more wondering about whether we have waited long enough. And we can actually stop the server the same way
sudo -u postgres pg_ctl start -w -D ${PGDATA}
sudo -u postgres psql --command "CREATE USER user WITH SUPERUSER PASSWORD 'user';" &&\
sudo -u postgres createdb -O user app
sudo -u postgres pg_ctl stop -w
Had the same problem inside a script in entry point. Following is my Dockerfile excerpt
# init execution
ENTRYPOINT ["/usr/local/sbin/initpostgres.sh"]
with following commands inside the intipostgres.sh script
su postgres -c "pg_ctl start -l /var/lib/postgresql/logpostgres"
su postgres -c "createuser -s $OPENERPUSER"
adding sleep 1 before the createuser command per #seanmcl suggested correction worked for me :
su postgres -c "pg_ctl start -l /var/lib/postgresql/logpostgres"
sleep 1
su postgres -c "createuser -s $OPENERPUSER"
The problem seems to be that the postgres unix socket is not on your host machine. You can fix this by running the following command.
docker run -p 5432:5432 --volume="/run/postgresql:/run/postgresql" -d --name postgres postgres
The essential part is the --volume flag. It links the folder that includes the unix socket file .s.PGSQL.5432 to the host machine in order to be read by other processes.
This seems like a duplicate question of Installing PostgreSQL within a docker container, is that right OP?

Fresh PG install on Ubuntu [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am setting up a Digital Ocean Ubuntu server to host my new site on and having trouble setting up postgres.
I installed it fine but when I try to access it using psql I get:
psql: FATAL: role "demo" does not exist
A few quick questions:
Has installing postgres create an standard ubuntu user account?
How do I create a new postgres account, I have tried createuser but get the following:
demo#dubbo:~$ createuser
Enter name of role to add: demopg
Shall the new role be a superuser? (y/n) y
createuser: could not connect to database postgres: FATAL: role "demo" does not exist
I guess that once I have a postgres user I should then be able to create a db for my application to use.
Here are the steps I use to install PostgreSQL on Ubuntu for development (I'm using PostreSQL with Rails):
$ sudo apt-get install postgresql postgresql-contrib libpq-dev
We need libpq-dev to be able to install the Ruby pg gem. After the setup completes,
then run the following commands:
$ sudo -u postgres createuser --superuser $USER
$ sudo -u postgres createuser --superuser demo
$ sudo -u postgres psql postgres
The first command creates a user that matches your user name. The second command creates a user for your application to use--this is what you will normally login to psql with. The third command will bring you directly into the psql console, and will open up the postgres database. It's just the database corresponding to the postgres user, which for some reason is necessary when installing PostgreSQL.
Once in the psql prompt, type the following command:
postgres=# \password demo
Set the password for your user. Once it returns you to the prompt, type "\q" to exit the psql prompt.
Now you'll be back in your terminal, and you can then create a database for your application.
$ createdb your_db_name
Now, you need to change the postgresql.conf file to make PostgreSQL listen on at least localhost. If you have a setup where you want to listen on an external IP or something, change this line to either the IP or just '*'.
/etc/postgresql/9.1/main/postgresql.conf:
listen_addresses = 'localhost'
Then, in pg_hba.conf, make sure you've got this:
/etc/postgresql/9.1/main/pg_hba.conf:
# "local" is for Unix domain socket connections only
local all all md5
Otherwise you'll probably get an error when trying to login from your Rails app. After this, all you've got to do is simply start PostgreSQL using this command:
$ sudo /etc/init.d/postgresql start
And you're set to go! Start psql with:
$ psql -U demo
You will be prompted for the password you set. You can connect to the database you created with:
# \c your_db_name
Good luck!