Creation a Database PostgreSQL 9.3 in Ubuntu - postgresql

I'm trying to create a database PostgreSQL 9.3 in Ubuntu 14.04 LTS. I've installed the database and now I'm trying to create an user, password and a new database. This is what I've done in the Ubuntu shell:
Installation
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get install postgresql-9.3
Creation of the user "myuser"
sudo -u postgres createuser myuser
Creation of the password
sudo -u postgres psql
postgres=# \password
Enter new password: bla bla...
Enter it again: bla bla...
Creation of the database "newdb"
createdb newdb -O myuser
But I get this error
createdb: could not connect to database template1: FATAL: role "bla bla" does not exist
what can be the issue?

You need to delete and re-create your user as follows :
dropuser myuser
createuser -l myser -P
You will be prompted for the password.
That should do the trick. Hope this helps.

Ok, verify your pg_hba.conf.
First run :
locate pg_hba.conf
This should return the location of the postgresql host based authentifcation configuration file.
Then check that the following line is set accordingly:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
This should enable passwordless access to your DB. You can try with:
psql -U postgres
Try it and let me know if you can connect.

Related

Database backup from Postgres

I have postgresql installed in my Ubuntu 16.04 system. I am trying to take back up of database but it does not work,
user1#rajeshN ~ $ sudo pg_dump -U postgres teleshop_development1 > pg_backup
[sudo] password for user1:
pg_dump: [archiver (db)] connection to database "teleshop_development1" failed: FATAL: Peer authentication failed for user "postgres"
So I switched to postgres, And tried there but I do not know password
user1#rajeshN ~ $ sudo -i -u postgres
postgres#rajeshN:~$
postgres#rajeshN:~$ sudo pg_dump -U postgres teleshop_development1 > pg_backup
[sudo] password for postgres:
Sorry, try again.
Please suggest how can I get password or back up. Thanks in advance!
When you are already the user "postgres", do not use sudo again. Its just
postgres#rajeshN:~$ pg_dump teleshop_development1 > pg_backup
Or you can do it all in one line
user1#rajeshN ~ $ sudo -u postgres pg_dump teleshop_development1 > pg_backup
Specify PostgreSQL password, EG with -W.
See: https://www.postgresql.org/docs/9.4/app-pgdump.html

How to enable regular linux users to access postgres database without sudo access?

We have a postgres database server(10.3) installed on Centos7, and created a database with the name of "db_name". We have database access. The setting in pg_hba.conf is as the following:
# "local" is for Unix domain socket connections only
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
When I have sudo access, I can access the database "db_name".
[root#localhost bin]# sudo -s
[root#localhost bin]# psql -U db_name db_user
psql (10.3)
Type "help" for help.
db_name=>
When I tried to access database as a regular linux user, I got the following error:
[linuxuser#localhost bin]# psql -U db_name db_user
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"?
The reason we want to limit some users sudo access is that our report specialists need an access to the database "db_name", but we don't want them to have the sudo privilege to do something else.
What kind of settings shall I do in order to make it work?
Thanks!
I found the solution.
The cause of the problem is that there is no /var/run/postgresql/.s.PGSQL.5432 file existed.
By default, unix_socket_directories is '/tmp' which is set in postgresql.conf.
For some reason, the regular linux user is not looking at /tmp/.s.PGSQL.5432, but instead looking at /var/run/postgresql/.s.PGSQL.5432 file.
So the fix is as the following:
cd /var/run
mkdir postgresql
cd postgresql/
ln -s /tmp/.s.PGSQL.5432 .s.PGSQL.5432
The following command works as well.
psql -h /tmp -U db_user db_name
In PostgreSQL you should create a role to access the DB.
For that you will have to do this:
Change to postgres account (Created during installation of postgresql)
$ sudo -i -u postgres
Create a new role
postgres#server createuser --interactive
Output
Enter name of role to add: DB_Name
Shall the new role be a superuser? (y/n) y
Create Database
postgres#server createdb DB_Name
Create user, change to user and access to database
$ sudo adduser DB_Name
$ sudo -i -u DB_Name
$ psql
DB_Name=# \conninfo
References:
How To Install and Use PostgreSQL on CentOS 7

Ecto Postgres install error password authentication failed

I created a phoenix project from the hello example using digital ocean. I entered the username and password from the etc/motd.tail file. I keep getting the error message below. I am a beginner and for some reason I just cannot get ecto to install correctly.
** (Mix) The database for Hello.Repo couldn't be created, reason given: psql: FATAL: password authentication failed for user "elixir"
FATAL: password authentication failed for user "elixir"
You can use the following Postgress database credentials:
* User: elixir
* Pass: ***
install. Any help would be appreciated.
I get the same error using Ubuntu 14.04 and I corrected resetting the 'postgres' password:
$ sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
and restart postgres service:
sudo service postgresql restart
I assume this error is happening on the mix ecto.create task?
This happens because Ecto uses psql to create the database, however this is no longer the case in the upcoming Ecto 2.0.
The following GitHub issue shows the same issue https://github.com/elixir-lang/ecto/issues/1207
The relevant comment with the fix is https://github.com/elixir-lang/ecto/issues/1207#issuecomment-172570064:
My database config (pg_hba.conf) was apparently wrong.
For anyone else encountering this:
host all my_user 127.0.0.1/32 trust will not work
host all my_user localhost trust will work
Please check your pg_hba.conf (likely in /etc/postsgresql/9.x/pg_hba.conf).
We just need to create a new postgresql username and password according to the files inside config folder using this db method
$ sudo -u postgres createuser <username>
$ sudo -u postgres createdb <dbname>
$ sudo -u postgres psql
psql=# alter user <username> with encrypted password '<password>';
psql=# grant all privileges on database <dbname> to <username> ;
I needed to update the pg_hba.conf to make this work.
I am using Fedora, so get to /var/lib/pgsql/data
# "local" is for Unix domain socket connections only
local all postgres peer
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 ident
Then I created an elixir user in postgres with databse creation capabilities and configured it in dev.exs (user/password/database)

Create local Postgresql account

On OS X 10.11.2, I've installed Postgres.app and I'm running the local server. I'm trying to create a local account with a username and password so that I can develop a Rails app locally. However, running the following command:
sudo -u postgres createuser -s {USERNAME}
I receive sudo: unknown user: postgres error.
Any suggestions as to why this error occurs and how to resolve this?
sudo tells you there is no system user "postgres".
When you installed PostgreSQL, it should have created database user "postgres" and you can try use that:
$ psql -u postgres
postgres=# create user {username} password '{password}';
I managed to create a user with the following steps.
In the terminal:
createuser --superuser {USERNAME}
I then set a password in psql:
\password {USERNAME}

role "postgres" does not exist; cannot createuser

I am on Linux Mint Cinnamon 2.2.16.
During the process of getting Rails up and running, I am having problems with Postgres.
postgres#BL ~ $ psql --version
psql (Postgres-XC) 1.1
(based on PostgreSQL) 9.2.4
I was unable to get anything working under my usual username, so I changed to the default user using
sudo su - postgres
I cannot get anything to work with createuser.
postgres#BL ~ $ psql
psql: FATAL: role "postgres" does not exist
postgres#BL ~ $ createuser -s -U $USER
createuser: could not connect to database postgres: FATAL: role "postgres" does not exist
postgres#BL ~ $ sudo -u postgres createuser newname
Sorry, user postgres is not allowed to execute '/usr/bin/createuser newname' as postgres on BL.
postgres#BL ~ $ which psql
/usr/bin/psql
postgres#BL ~ $ psql \l
psql: FATAL: role "postgres" does not exist
After thoroughly researching the problem and tearing out more than a few hairs, I decided this was some variation of a problem with packaging/installation, similar problem noted here: unable to create user postgres: role "postgres" does not exists
I did a complete uninstall, as per below, and reinstalled without postgresql-xc
How to thoroughly purge and reinstall postgresql on ubuntu?
The new install had expected behavior with the user "postgres" and I was able to add myself as a superuser and create new databases. After some post-installation finagling, Rails seems to be running and playing nice with postgres.
You have to authenticate to psql as the superuser in order to manage users.
For example
psql -U root
Afterwards
create user paige with password 'paige';