This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Postgresql not creating db with “createdb” as superuser, yet not outputting errors [duplicate]
(5 answers)
Closed 1 year ago.
I'm trying to create a database in PostgreSQL using this command:
createdb -E UTF8 aws-inventories
There are no errors from that command. But when I go to list the dbs the new database isn't there:
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-------------------+----------+----------+----------------------------+----------------------------+-----------------------
analysis | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
animals | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
postgis_31_sample | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(6 rows)
And if I try to change to that new database it tells me that it isn't there:
postgres-# \c aws-inventories
FATAL: database "aws-inventories" does not exist
Previous connection kept
I also tried this with the CREATE DATABASE command. And I get the same result:
postgres=# CREATE DATABASE aws-inventories
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-------------------+----------+----------+----------------------------+----------------------------+-----------------------
analysis | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
animals | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
postgis_31_sample | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(6 rows)
postgres-# \c aws-inventories
FATAL: database "aws-inventories" does not exist
Previous connection kept
Doing the same commands in PGAdmin4 I do get an error:
createdb aws-inventories
ERROR: syntax error at or near "createdb"
LINE 1: createdb aws-inventories
^
SQL state: 42601
Character: 1**strong text**
CREATE DATABASE aws-inventories
ERROR: syntax error at or near "-"
LINE 1: CREATE DATABASE aws-inventories
^
SQL state: 42601
Character: 20
I'm on PostgreSQL 13. Why is Postgres refusing to create this database?
Related
I try to switch with powershell from databases with postgres (14.1)
So I do this:
postgres-# \l
And then I see all the databases. But for example how can I now switch to the test database?
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+--------------------------+--------------------------+-----------------------
postgres | postgres | UTF8 | English_Netherlands.1252 | English_Netherlands.1252 |
template0 | postgres | UTF8 | English_Netherlands.1252 | English_Netherlands.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_Netherlands.1252 | English_Netherlands.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | English_Netherlands.1252 | English_Netherlands.1252 |
(4 rows)
I try it like this:
postgres-# -d --test
But that doesn't work.
Use \connect (or just \c)
Quote from the manual
\c or \connect
Establishes a new connection to a PostgreSQL server. The connection parameters to use can be specified either using a positional syntax (one or more of database name, user, host, and port), or using a conninfo connection string as detailed in Section 34.1.1. If no arguments are given, a new connection is made using the same parameters as before.
The -d parameter can only be used when starting psql:
PS c:\> psql -d test
I have a problem where I want to create two databases
and two schemas. Each database will have a separate schema.
The public schema has been dropped. I have tried this without dropping
the public schema with the same result. When connecting to the database with the database and schema owner the schema cannot be seen or used.
I have set up single databases with an schema, but never tried to set up more than one. The problem sees to come from multiple databases and schemas.
create user tom;
create database fishes owner tom encoding = 'UTF8';
create schema fish authorization tom;
alter database fishes set schema 'fish';
create user harry;
create database lizards owner harry encoding = 'UTF8';
create schema lizard authorization harry;
alter database lizards set schema 'lizard';
psql (10.8)
postgres=# create user tom;
CREATE ROLE
postgres=# create database fishes owner tom encoding = 'UTF8';
CREATE DATABASE
postgres=# create schema fish authorization tom;
CREATE SCHEMA
postgres=# alter database fishes set schema 'fish';
ALTER DATABASE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
tom | | {}
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
fishes | tom | UTF8 | English_United States.1252 | English_United States.1252 |
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# \dn
List of schemas
Name | Owner
------+-------
fish | tom
(1 row)
postgres=# create user harry;
CREATE ROLE
postgres=# create database lizards owner harry encoding = 'UTF8';
CREATE DATABASE
postgres=# create schema lizard authorization harry;
CREATE SCHEMA
postgres=# alter database lizards set schema 'lizard';
ALTER DATABASE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
harry | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
tom | | {}
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
fishes | tom | UTF8 | English_United States.1252 | English_United States.1252 |
lizards | harry | UTF8 | English_United States.1252 | English_United States.1252 |
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
postgres=# \dn
List of schemas
Name | Owner
--------+-------
fish | tom
lizard | harry
(2 rows)
postgres=# alter user tom password 'xxx';
ALTER ROLE
postgres=# alter user harry password 'xxx';
ALTER ROLE
when I did a \dnpsql -U tom fishes
Password for user tom:
psql (10.8)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
fishes=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
fishes | tom | UTF8 | English_United States.1252 | English_United States.1252 |
lizards | harry | UTF8 | English_United States.1252 | English_United States.1252 |
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
fishes=> \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
harry | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
tom | | {}
fishes=> \dn
List of schemas
Name | Owner
--------+----------
public | postgres
(1 row)
fishes=> show search_path;
search_path
-------------
fish
(1 row)
fishes=> create table eels( type varchar(30) primary key );
ERROR: no schema has been selected to create in
LINE 1: create table eels( type varchar(30) primary key );
^
fishes=> create table fish.eels( type varchar(30) primary key );
ERROR: schema "fish" does not exist
LINE 1: create table fish.eels( type varchar(30) primary key );
^
I would expect when I logged in with either user that when I logged in with one of my non-postgres users that \dn would show the schema for the database that is show with the show search_path. Search_path is correct. Also it seems that access is not granted to the owner of the schema.
The answer is as Jeremy suggested in his comment.
Before creating the schema connect to the database you want to create the schema in.
There is no create schema syntax that includes the database name it should be created in, so it needs to be done this way.
$ sudo -u postgres psql
postgres=# \list
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------+----------+----------+-------------+-------------+-----------------------
linuxhowtodb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | |
How can I enter the database linuxhowtodb without leaving the current psql session? Thanks.
Note that if I can leave the current psql session, then I can run the following to access a particular database:
$ psql linuxhowtodb
psql (9.6.5)
Type "help" for help.
linuxhowtodb=>
Use \c:
postgres=# \c linuxhowtodb
From the documentation:
\c or \connect [ -reuse-previous=on|off ] [ dbname [ username ] [ host ] [ port ] | conninfo ]
Establishes a new connection to a PostgreSQL server. The connection parameters to use can be specified either using a positional syntax, or using conninfo connection strings.
What's wrong with my usage of ansible postgresql_user:
- name: Create user and grant access to the database
become_user: postgres
become: yes
postgresql_user:
db: '{{db_name}}'
name: '{{db_user}}'
password: '{{db_password}}'
priv: ALL
state: present
The database with proper name is created and when I enter psql being postgres user:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+-------------+-------------+-----------------------
app-andi | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | andi=CTc/postgres
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
problem:
when trying to:
psql -d "app-andi" -U andi -W
and providing password given in ansible variable I end up with:
psql: FATAL: Peer authentication failed for user "andi"
USING:
ansible 2.3.1.0
Vagrant 1.9.4
DOCS:
http://docs.ansible.com/ansible/latest/postgresql_user_module.html
https://www.postgresql.org/docs/9.2/static/app-psql.html
problem was completly ansible un-related.
Just connect like this:
psql -d "app-andi" -U andi -W -h 127.0.0.1
I’m totally new to developer world, so I apologize if I’m not make my question clearly, please don’t be hesitate to point out if I did any wrong. Thanks.
I faced this problem on setting my PostgreSQL DB, no relations found when type command timlin=# \d.
I did try below solution to fix but it didn’t work.
postgresql database owner can't access database - "No relations found."
Below is my situation
timlin=# \dn+
List of schemas
Name | Owner | Access privileges | Description
--------+--------+-------------------+————————————
public | timlin | timlin=UC/timlin +| standard public schema
| | =UC/timlin |
(1 row)
timlin=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------------+--------+----------+-------------+-------------+--------------------
postgres | timlin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
psqlapp | timlin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
psqlapp_development | timlin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
psqlapp_test | timlin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
psqlappdemo | timlin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/timlin +
| | | | | timlin=CTc/timlin +
| | | | | psqlapp=CTc/timlin
template0 | timlin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/timlin +
| | | | | timlin=CTc/timlin
template1 | timlin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/timlin +
| | | | | timlin=CTc/timlin
timlin | timlin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(8 rows)
timlin=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
psqlapp | | {}
timlin | Superuser, Create role, Create DB, Replication | {}
I check each one and couldn't find any difference between previous soultion.
While I try
timlin=# \d
I still got the result :
No relations found.
What do I missed ? How can I fix it ?
Please advice and thank you all in advence.
This would imply that the database timlin does not contain any tables in the public schema (which is what \d would normally list).
It sounds like you created an empty database, e.g.:
createdb -h localhost -U postgres timlin
In which case, immediately after creation, \d would return:
No relations found.
You would need to explicitly create one or more tables in order to see anything listed in the output of \d.
e.g. if you do the following:
create table foo (id serial, val text);
And then do a \d, the following output will result:
List of relations
Schema | Name | Type | Owner
--------+------------+----------+----------
public | foo | table | postgres
public | foo_id_seq | sequence | postgres
(2 rows)